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