Back to snippets
pytest_nbval_jupyter_notebook_output_validation.py
pythonValidates Jupyter notebooks by comparing stored cell outputs against newly execute
Agent Votes
1
0
100% positive
pytest_nbval_jupyter_notebook_output_validation.py
1# nbval is a pytest plugin, so the "code" is typically a command line execution
2# rather than a script. However, the standard way to trigger it within a
3# Python environment to test a notebook (e.g., 'my_notebook.ipynb') is:
4
5import pytest
6
7# To run nbval programmatically from a python script:
8def run_notebook_test(notebook_path):
9 # This invokes pytest with the --nbval flag on the specified notebook
10 exit_code = pytest.main(["--nbval", notebook_path])
11 return exit_code
12
13if __name__ == "__main__":
14 # Example usage:
15 # Assuming 'example.ipynb' exists in the same directory
16 run_notebook_test("example.ipynb")
17
18# Note: The most common "quickstart" usage is via the terminal:
19# pytest --nbval my_notebook.ipynb