Skip to content

Commit 07649a9

Browse files
committed
Use pytest_notebook to test whether notebooks run without errors
1 parent 89c23c4 commit 07649a9

File tree

2 files changed

+14
-41
lines changed

2 files changed

+14
-41
lines changed

example/test_examples.py

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
"""
55

66
import os
7-
import subprocess
8-
import tempfile
9-
import nbformat
10-
import sys
117
from example import modelchain_example as mc_e
128
from example import turbine_cluster_modelchain_example as tc_mc_e
139
from numpy.testing import assert_allclose
14-
import pytest
10+
import pytest_notebook
1511

1612

1713
class TestExamples:
@@ -57,50 +53,27 @@ def test_turbine_cluster_modelchain_example_flh(self):
5753

5854
def _notebook_run(self, path):
5955
"""
60-
Execute a notebook via nbconvert and collect output.
56+
Execute a notebook and collect output.
6157
Returns (parsed nb object, execution errors)
6258
"""
63-
dirname, __ = os.path.split(path)
64-
os.chdir(dirname)
65-
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
66-
args = [
67-
"jupyter",
68-
"nbconvert",
69-
"--to",
70-
"notebook",
71-
"--execute",
72-
"--ExecutePreprocessor.timeout=60",
73-
"--output",
74-
fout.name,
75-
path,
76-
]
77-
subprocess.check_call(args)
78-
79-
fout.seek(0)
80-
nb = nbformat.read(fout, nbformat.current_nbformat)
81-
82-
errors = [
83-
output
84-
for cell in nb.cells
85-
if "outputs" in cell
86-
for output in cell["outputs"]
87-
if output.output_type == "error"
88-
]
89-
90-
return nb, errors
59+
notebook = pytest_notebook.notebook.load_notebook(path=path)
60+
result = pytest_notebook.execution.execute_notebook(
61+
notebook,
62+
with_coverage=False,
63+
timeout=600,
64+
)
65+
return result.exec_error
9166

92-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
9367
def test_modelchain_example_ipynb(self):
9468
dir_path = os.path.dirname(os.path.realpath(__file__))
95-
nb, errors = self._notebook_run(
69+
errors = self._notebook_run(
9670
os.path.join(dir_path, "modelchain_example.ipynb")
9771
)
98-
assert errors == []
72+
assert errors is None
9973

100-
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
10174
def test_turbine_cluster_modelchain_example_ipynb(self):
10275
dir_path = os.path.dirname(os.path.realpath(__file__))
103-
nb, errors = self._notebook_run(
76+
errors = self._notebook_run(
10477
os.path.join(dir_path, "turbine_cluster_modelchain_example.ipynb")
10578
)
106-
assert errors == []
79+
assert errors is None

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def read(fname):
3030
"pytest",
3131
"jupyter",
3232
"sphinx_rtd_theme",
33-
"nbformat",
3433
"numpy",
3534
"matplotlib",
35+
"pytest-notebook",
3636
]
3737
},
3838
)

0 commit comments

Comments
 (0)