Skip to content

Commit

Permalink
Testing: Add pueblo.testing.notebook.{list_path,generate_tests}
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Mar 7, 2024
1 parent a8ca065 commit 431af8d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes for pueblo

## Unreleased
- Testing: Add `pueblo.testing.notebook.{list_path,generate_tests}`

## 2024-02-10 v0.0.8
- ngr: Add capability to invoke Python's built-in `unittest`
Expand Down
26 changes: 26 additions & 0 deletions pueblo/testing/notebook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing as t
from pathlib import Path

import pytest

Expand Down Expand Up @@ -47,3 +48,28 @@ async def async_execute_cell(
raise

NotebookClient.async_execute_cell = async_execute_cell # type: ignore[method-assign,unused-ignore]


def list_path(path: Path, suffix: str = ".ipynb"):
"""
Enumerate all Jupyter Notebook files found in given directory.
"""
for item in path.iterdir():
if item.suffix == suffix:
yield item


def generate_tests(metafunc, paths: t.Union[t.List[Path], None] = None, path: t.Union[Path, None] = None):
"""
Generate test cases for Jupyter Notebooks.
To be used from `pytest_generate_tests`.
"""
if path:
paths = list(list_path(path))
elif paths:
paths = list(paths)
else:
raise ValueError("Path is missing")
if "notebook" in metafunc.fixturenames:
names = [nb_path.name for nb_path in paths]
metafunc.parametrize("notebook", paths, ids=names)
19 changes: 18 additions & 1 deletion tests/test_code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from pueblo.testing.notebook import monkeypatch_pytest_notebook_treat_cell_exit_as_notebook_skip
from pueblo.testing.notebook import generate_tests, monkeypatch_pytest_notebook_treat_cell_exit_as_notebook_skip
from pueblo.testing.snippet import pytest_module_function, pytest_notebook

HERE = Path(__file__).parent
Expand Down Expand Up @@ -95,3 +95,20 @@ def test_notebook_patching():
tb.execute()
output = tb.cell_output_text(6)
assert output == "33.33"


def pytest_generate_tests(metafunc):
"""
Generate test cases for Jupyter Notebooks, one test case per .ipynb file.
"""
generate_tests(metafunc, path=TESTDATA_FOLDER)


def test_notebook(notebook):
"""
Execute Jupyter Notebook, one test case per .ipynb file.
"""
from testbook import testbook

with testbook(notebook) as tb:
tb.execute()

0 comments on commit 431af8d

Please sign in to comment.