-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_notebooks.py
49 lines (40 loc) · 1.72 KB
/
test_notebooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Test that all notebooks in the tutorial run without error"""
import os
import pytest
import subprocess
import sys
def _notebook_run(filepath):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
# _, name = os.path.split(filepath)
# name = os.path.splitext(name)[0]
# outfilename = os.path.join(outdir, '{}.ipynb'.format(name))
args = [sys.executable, '-m', 'jupyter', 'nbconvert', '--log-level', 'WARN',
'--to', 'notebook', '--execute',
'--ExecutePreprocessor.timeout=300', # 5 min max for executing
'--stdout', filepath]
return subprocess.check_call(args, stdout=subprocess.DEVNULL)
def _find_ipynbs():
"""Finds all the jupyter notebooks present in the tutorial directory"""
tutorial_dir = os.path.abspath(os.path.join(__file__, '../../../tutorial'))
ipynb_filepaths = []
for root, dirs, files in os.walk(tutorial_dir):
for filename in files:
if filename.endswith('.ipynb') and os.path.split(root)[1] != '.ipynb_checkpoints':
ipynb_filepaths.append(os.path.join(root, filename))
return ipynb_filepaths
@pytest.mark.parametrize("filepath", _find_ipynbs(), ids=os.path.basename)
def test_ipynb(filepath):
old_python_path = os.getenv("PYTHONPATH")
# pytest populates sys.path to access the tested module
os.environ["PYTHONPATH"] = os.pathsep.join(sys.path)
_notebook_run(filepath)
if old_python_path is not None:
os.environ["PYTHONPATH"] = old_python_path
else:
del os.environ["PYTHONPATH"]
if __name__ == '__main__':
for filepath in _find_ipynbs():
print('Running {}'.format(filepath))
_notebook_run(filepath)