|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import os |
7 | | -import subprocess |
8 | | -import tempfile |
9 | | -import nbformat |
10 | | -import sys |
11 | 7 | from example import modelchain_example as mc_e |
12 | 8 | from example import turbine_cluster_modelchain_example as tc_mc_e |
13 | 9 | from numpy.testing import assert_allclose |
14 | | -import pytest |
| 10 | +import pytest_notebook |
15 | 11 |
|
16 | 12 |
|
17 | 13 | class TestExamples: |
@@ -57,50 +53,27 @@ def test_turbine_cluster_modelchain_example_flh(self): |
57 | 53 |
|
58 | 54 | def _notebook_run(self, path): |
59 | 55 | """ |
60 | | - Execute a notebook via nbconvert and collect output. |
| 56 | + Execute a notebook and collect output. |
61 | 57 | Returns (parsed nb object, execution errors) |
62 | 58 | """ |
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 |
91 | 66 |
|
92 | | - @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6") |
93 | 67 | def test_modelchain_example_ipynb(self): |
94 | 68 | dir_path = os.path.dirname(os.path.realpath(__file__)) |
95 | | - nb, errors = self._notebook_run( |
| 69 | + errors = self._notebook_run( |
96 | 70 | os.path.join(dir_path, "modelchain_example.ipynb") |
97 | 71 | ) |
98 | | - assert errors == [] |
| 72 | + assert errors is None |
99 | 73 |
|
100 | | - @pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6") |
101 | 74 | def test_turbine_cluster_modelchain_example_ipynb(self): |
102 | 75 | dir_path = os.path.dirname(os.path.realpath(__file__)) |
103 | | - nb, errors = self._notebook_run( |
| 76 | + errors = self._notebook_run( |
104 | 77 | os.path.join(dir_path, "turbine_cluster_modelchain_example.ipynb") |
105 | 78 | ) |
106 | | - assert errors == [] |
| 79 | + assert errors is None |
0 commit comments