Skip to content

WIP:feat/notebook tests#255

Open
nakul680 wants to merge 4 commits into
faccts:mainfrom
nakul680:feat/notebook-tests
Open

WIP:feat/notebook tests#255
nakul680 wants to merge 4 commits into
faccts:mainfrom
nakul680:feat/notebook-tests

Conversation

@nakul680

@nakul680 nakul680 commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Closes Issues

Closes #202

Description

  • Adds a test script that runs all tutorial notebooks( with a few exceptions of notebooks that rely on external packages) and checks whether these notebooks are executed without issues.

Release Notes

(Project adheres to Keep a Changelog; Every entry should start in upper-case and end with (#<pr-id>); Remove sections that don't apply)

Added

@nakul680 nakul680 self-assigned this Jul 1, 2026
@nakul680 nakul680 requested a review from a team as a code owner July 1, 2026 12:05
@nakul680 nakul680 added the testsuite Concerning the testsuite of OPI label Jul 1, 2026

@timmyte timmyte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. It's definitely about time we also test the notebooks.
We should just polish the architecture a little.

Comment thread tests/test_notebook.py


def run_notebook(nb: Path) -> tuple[bool, float, str]:
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please complete the docstring.

Comment thread tests/test_notebook.py

"""
start = time.monotonic()
with tempfile.TemporaryDirectory() as tmp:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe in detail what you are doing here:

  • Where are notebooks executed?
  • How are they executed?
  • Why is there a timeout?
  • Why does it need to copied over?

Where do make sure jupyter is installed?

Comment thread tests/test_notebook.py
"--ExecutePreprocessor.timeout=1800",
"--output",
nb.name,
"--output-dir",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess --output-dir is redundant, as cwd=tmp.

Comment thread tests/test_notebook.py
Function to iteratively test whether all notebooks (with exception to the ones to be excluded, which are defined in `BlACKLIST`) execute
without issues. In the case that a notebook execution fails,the function will print out which notebooks fail and the error message.
"""
notebooks = [nb for nb in NOTEBOOKS_DIR.glob("*.ipynb") if nb.stem not in BLACKLIST]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should actually be a fixture.

Instead of one master function like here that acts as whole notebooks testsuite, I would boil this down to a single function, that takes a notebook, executes it and returns a flag if the notebook failed or not.

I would also keep the reporting to a minimum. Pytests captures the output anyway.
Though we should keep the printing of the timings. This is good for debugging and make sure out testsuite does not take too long ;)

I think @haneug already wrote a custom summary report for one type of tests that we have. Maybe he can help.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the fixture / parametrize. I did add a custom error message when example tests fail although I do not remember much of this I am happy to discuss this.

Comment thread tests/test_notebook.py
cwd=tmp,
)
elapsed = time.monotonic() - start
return result.returncode == 0, elapsed, result.stdout + result.stderr

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea with the boolean!

Comment thread tests/test_notebook.py
import pytest

NOTEBOOKS_DIR = Path(__file__).parent.parent / "docs/contents/notebooks"
BLACKLIST = ["extopt", "ir_spectrum", "ml_properties"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are they blacklisted?
Please add a comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they require additional external dependencies that cannot easily be set up by hand (extopt and ml_properties at least). This should be documented here. About ir_spectrum I am not sure?

Comment thread tests/test_notebook.py

import pytest

NOTEBOOKS_DIR = Path(__file__).parent.parent / "docs/contents/notebooks"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoding these complex paths, I think we should start adding symlinks to the repo. If we refactor the layout at some point, finding broken symlinks, is a peace of cake, but correcting the hardcoded paths is painful.

@haneug
Objections?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sounds good!

Comment thread tests/test_notebook.py

import pytest

NOTEBOOKS_DIR = Path(__file__).parent.parent / "docs/contents/notebooks"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sounds good!

Comment thread tests/test_notebook.py
import pytest

NOTEBOOKS_DIR = Path(__file__).parent.parent / "docs/contents/notebooks"
BLACKLIST = ["extopt", "ir_spectrum", "ml_properties"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they require additional external dependencies that cannot easily be set up by hand (extopt and ml_properties at least). This should be documented here. About ir_spectrum I am not sure?

Comment thread tests/test_notebook.py
return result.returncode == 0, elapsed, result.stdout + result.stderr


@pytest.mark.notebooks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notebooks also always run orca so I guess @pytest.mark.orca is also appropriate here

Comment thread tests/test_notebook.py
Function to iteratively test whether all notebooks (with exception to the ones to be excluded, which are defined in `BlACKLIST`) execute
without issues. In the case that a notebook execution fails,the function will print out which notebooks fail and the error message.
"""
notebooks = [nb for nb in NOTEBOOKS_DIR.glob("*.ipynb") if nb.stem not in BLACKLIST]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the fixture / parametrize. I did add a custom error message when example tests fail although I do not remember much of this I am happy to discuss this.

Comment thread tests/test_notebook.py
@pytest.mark.notebooks
def test_notebooks() -> None:
"""
Function to iteratively test whether all notebooks (with exception to the ones to be excluded, which are defined in `BlACKLIST`) execute

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in BlACKLIST

Comment thread tests/test_notebook.py
notebooks = [nb for nb in NOTEBOOKS_DIR.glob("*.ipynb") if nb.stem not in BLACKLIST]
if not notebooks:
print(f"No valid notebooks found in {NOTEBOOKS_DIR}")
sys.exit(1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use pytest.fail() instead

@nakul680 nakul680 changed the title Feat/notebook tests WIP:feat/notebook tests Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testsuite Concerning the testsuite of OPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run tutorial notebooks as part of the tests

3 participants