Spaces:
Running
Running
File size: 1,494 Bytes
5301c48 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
"""
Helper module to discover and run test notebooks with pytest.
This module is optional. If you want to run notebook tests directly, you can use:
pytest path/to/notebook.ipynb
"""
import os
import pytest
from pathlib import Path
def get_notebooks(base_dir=None):
"""Find all test notebooks in the project directory."""
if base_dir is None:
base_dir = Path(__file__).parent
else:
base_dir = Path(base_dir)
notebooks = []
for nb_path in base_dir.rglob("*.ipynb"):
# Skip checkpoints
if ".ipynb_checkpoints" in str(nb_path):
continue
# Skip specific notebook
if "data_factory.ipynb" in str(nb_path):
continue
# Only include notebooks that follow test naming convention
if nb_path.name.startswith("test_"):
notebooks.append(str(nb_path))
return notebooks
@pytest.mark.notebook
@pytest.mark.parametrize("notebook_file", get_notebooks())
def test_notebook_execution(notebook_file):
"""Run the notebook through pytest to verify it executes without errors."""
pytest.importorskip("nbval")
if "data_factory.ipynb" in notebook_file:
pytest.skip("Skipping data_factory.ipynb as it is excluded from testing")
# This test will be collected by pytest
# We just need to ensure the file exists
assert os.path.exists(notebook_file), f"Notebook file not found: {notebook_file}"
# The actual testing of the notebook is handled by nbval
|