|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import os |
7 | | -import subprocess |
8 | | -import tempfile |
9 | | -import nbformat |
10 | 7 | from example import modelchain_example as mc_e |
11 | 8 | from example import turbine_cluster_modelchain_example as tc_mc_e |
12 | 9 | from numpy.testing import assert_allclose |
| 10 | +import pytest_notebook |
13 | 11 |
|
14 | 12 |
|
15 | 13 | class TestExamples: |
@@ -55,48 +53,27 @@ def test_turbine_cluster_modelchain_example_flh(self): |
55 | 53 |
|
56 | 54 | def _notebook_run(self, path): |
57 | 55 | """ |
58 | | - Execute a notebook via nbconvert and collect output. |
59 | | - Returns (parsed nb object, execution errors) |
| 56 | + Execute a notebook and collect output. |
| 57 | + Returns execution errors. |
60 | 58 | """ |
61 | | - dirname, __ = os.path.split(path) |
62 | | - os.chdir(dirname) |
63 | | - with tempfile.NamedTemporaryFile(suffix=".ipynb", dir=dirname) as fout: |
64 | | - args = [ |
65 | | - "jupyter", |
66 | | - "nbconvert", |
67 | | - "--to", |
68 | | - "notebook", |
69 | | - "--execute", |
70 | | - "--ExecutePreprocessor.timeout=600", |
71 | | - "--output", |
72 | | - fout.name, |
73 | | - path, |
74 | | - ] |
75 | | - subprocess.check_call(args) |
76 | | - |
77 | | - fout.seek(0) |
78 | | - nb = nbformat.read(fout, nbformat.current_nbformat) |
79 | | - |
80 | | - errors = [ |
81 | | - output |
82 | | - for cell in nb.cells |
83 | | - if "outputs" in cell |
84 | | - for output in cell["outputs"] |
85 | | - if output.output_type == "error" |
86 | | - ] |
87 | | - |
88 | | - 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 |
89 | 66 |
|
90 | 67 | def test_modelchain_example_ipynb(self): |
91 | 68 | dir_path = os.path.dirname(os.path.realpath(__file__)) |
92 | | - nb, errors = self._notebook_run( |
| 69 | + errors = self._notebook_run( |
93 | 70 | os.path.join(dir_path, "modelchain_example.ipynb") |
94 | 71 | ) |
95 | | - assert errors == [] |
| 72 | + assert errors is None |
96 | 73 |
|
97 | 74 | def test_turbine_cluster_modelchain_example_ipynb(self): |
98 | 75 | dir_path = os.path.dirname(os.path.realpath(__file__)) |
99 | | - nb, errors = self._notebook_run( |
| 76 | + errors = self._notebook_run( |
100 | 77 | os.path.join(dir_path, "turbine_cluster_modelchain_example.ipynb") |
101 | 78 | ) |
102 | | - assert errors == [] |
| 79 | + assert errors is None |
0 commit comments