From 7b600eafada9e36c6c085a049d3f388cb02c4f9d Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl> Date: Wed, 4 Sep 2024 10:26:47 +0200 Subject: [PATCH] Use new setup --- .github/workflows/examples.yml | 6 ++++-- tests/test_tutorials.py | 28 ++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index b61bde698b3..9c2560d1b9c 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -50,8 +50,10 @@ jobs: cache: 'pip' - name: Install uv run: pip install uv - - name: Install dependencies - run: uv pip install --system . pytest nbval jupyter nbconvert + - name: Install Mesa dependencies + run: uv pip install --system . + - name: Install tutorial dependencies + run: uv pip install pytest jupyter nbconvert seaborn pandas # Run Pytest on Jupyter Notebooks - name: Test Jupyter Notebooks with Pytest run: pytest -rA -Werror -Wdefault::FutureWarning tests/test_tutorials.py diff --git a/tests/test_tutorials.py b/tests/test_tutorials.py index 18aea7ae7f5..3280ad5ad86 100644 --- a/tests/test_tutorials.py +++ b/tests/test_tutorials.py @@ -1,19 +1,23 @@ import glob +import nbformat import pytest +from nbconvert.preprocessors import ExecutePreprocessor -# Locate all notebooks in docs and subdirectories notebooks = glob.glob("docs/**/*.ipynb", recursive=True) -# Parameterize the test with all found notebooks -@pytest.mark.parametrize("notebook", notebooks) -def test_notebook_execution(notebook): - # This runs each notebook using nbval and ensures there are no errors - pytest.main( - [ - "--nbval", - "--nbval-current-env", # Use the current environment - notebook, - ] - ) +@pytest.mark.parametrize("notebook_path", notebooks) +def test_notebook_execution(notebook_path): + # Load the notebook + with open(notebook_path) as f: + nb = nbformat.read(f, as_version=4) + + # Set up the notebook execution + ep = ExecutePreprocessor(timeout=600, kernel_name="python3") + + try: + # Execute the notebook and catch any errors + ep.preprocess(nb, {"metadata": {"path": "./"}}) + except Exception as e: + pytest.fail(f"Notebook {notebook_path} failed: {e!s}")