Skip to content

Commit

Permalink
Use pytest_notebook to test whether notebooks run without errors
Browse files Browse the repository at this point in the history
  • Loading branch information
birgits committed Jan 12, 2024
1 parent 89c23c4 commit 07649a9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
53 changes: 13 additions & 40 deletions example/test_examples.py
Expand Up @@ -4,14 +4,10 @@
"""

import os
import subprocess
import tempfile
import nbformat
import sys
from example import modelchain_example as mc_e
from example import turbine_cluster_modelchain_example as tc_mc_e
from numpy.testing import assert_allclose
import pytest
import pytest_notebook


class TestExamples:
Expand Down Expand Up @@ -57,50 +53,27 @@ def test_turbine_cluster_modelchain_example_flh(self):

def _notebook_run(self, path):
"""
Execute a notebook via nbconvert and collect output.
Execute a notebook and collect output.
Returns (parsed nb object, execution errors)
"""
dirname, __ = os.path.split(path)
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = [
"jupyter",
"nbconvert",
"--to",
"notebook",
"--execute",
"--ExecutePreprocessor.timeout=60",
"--output",
fout.name,
path,
]
subprocess.check_call(args)

fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)

errors = [
output
for cell in nb.cells
if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"
]

return nb, errors
notebook = pytest_notebook.notebook.load_notebook(path=path)
result = pytest_notebook.execution.execute_notebook(
notebook,
with_coverage=False,
timeout=600,
)
return result.exec_error

@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_modelchain_example_ipynb(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
nb, errors = self._notebook_run(
errors = self._notebook_run(
os.path.join(dir_path, "modelchain_example.ipynb")
)
assert errors == []
assert errors is None

@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6")
def test_turbine_cluster_modelchain_example_ipynb(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
nb, errors = self._notebook_run(
errors = self._notebook_run(
os.path.join(dir_path, "turbine_cluster_modelchain_example.ipynb")
)
assert errors == []
assert errors is None
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -30,9 +30,9 @@ def read(fname):
"pytest",
"jupyter",
"sphinx_rtd_theme",
"nbformat",
"numpy",
"matplotlib",
"pytest-notebook",
]
},
)

0 comments on commit 07649a9

Please sign in to comment.