1 # coding: utf-8
2 """A `pytest` plugin for importing notebooks as modules and using standard test discovered.
3
4 The `AlternativeModule` is reusable. See `pidgin` for an example.
5 """
6
7 from pathlib import Path
8
9 import pytest
10
11 from importnb import Notebook
12
13
14 def get_file_patterns ( cls , parent ) :
15 for pat in parent . config . getini ( "python_files" ) : (empty)
16 for e in cls . loader ( ) . extensions : (empty)
17 yield "*" + pat . rstrip ( ".py" ) + e (empty)
18
19
20 class AlternativeModule ( pytest . Module ) :
21 def _getobj ( self ) :
22 return self . loader . load_file ( str ( self . path ) , False ) (empty)
23
24 @ classmethod
25 def pytest_collect_file ( cls , parent , path ) :
26 if not parent . session . isinitpath ( path ) : 26 ↛ 33 line 26 didn't jump to line 33, because the condition on line 26 was never false (empty)
27 for pat in get_file_patterns ( cls , parent ) : (empty)
28 if path . fnmatch ( pat ) : (empty)
29 break (empty)
30 else :
31 return (empty)
32
33 if hasattr ( cls , "from_parent" ) : 33 ↛ 35 line 33 didn't jump to line 35, because the condition on line 33 was never false (empty)
34 return cls . from_parent ( parent , path = Path ( path ) ) (empty)
35 return cls ( path , parent )
36
37
38 class NotebookModule ( AlternativeModule ) :
39 loader = Notebook
40
41
42 pytest_collect_file = NotebookModule . pytest_collect_file