Suppose I have a test expressed as a simple script with assert-statements (see background for why), e.g
import foo
assert foo(3) == 4
How would I include this script in my pytest test suite -- in a nice way?
I have tried two working but less-than-nice approaches:
One approach is to name the script like a test, but this makes the whole pytest discovery fail when the test fails.
My current approach is to import the script from within a test function:
def test_notebooks():
notebook_folder = Path(__file__).parent / 'notebooks'
for notebook in notebook_folder.glob('*.py'):
import_module(f'{notebook_folder.name}.{notebook.stem}')
This works, but the scripts are not reported individually and test failures have a long and winding stack trace:
__________________________________________________ test_notebooks ___________________________________________________
def test_notebooks():
notebook_folder = Path(__file__).parent / 'notebooks'
for notebook in notebook_folder.glob('*.py'):
> import_module(f'{notebook_folder.name}.{notebook.stem}')
test_notebooks.py:7:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
envs\anaconda\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1006: in _gcd_import
... (9 lines removed)...
<frozen importlib._bootstrap>:219: in _call_with_frames_removed
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> assert False
E AssertionError
notebooks\notebook_2.py:1: AssertionError
Background
The reason I have test in script files is that they are really Jupyter notebooks saved as .py-files with markup by the excellent jupytext plugin.
These notebooks are converted to html for documentation, can be used interactively for learning the system, and serve as cheap functional tests.