Skip to main content

pytest-notebook

CI Coverage RTD PyPI Conda

A pytest plugin for regression testing and regenerating Jupyter Notebooks.

Example Test


Purpose

The purpose of the plugin is to ensure that changes to the python environment (e.g. updating packages), have not affected the outputs of the notebook. If the notebook has changed, this plugin can also regenerate the notebooks, saving the new outputs.

Features

  • Recognise, collect, execute (optionally output) then diff input vs. output Jupyter Notebooks.

  • Provides clear and colorized diffs of the notebooks (using nbdime)

  • Regenerate failing notebooks.

  • Integration with coverage and pytest_cov.

  • A well defined API allows notebook regression tests to be run:

    1. Using the pytest test collection architecture.
    2. As a pytest fixtures (nb_regression.check(filename)).
    3. Using the pytest_notebook python package.
  • All stages are highly configurable via:

    1. The pytest command-line interface.
    2. The pytest configuration file.
    3. The notebook and cell level metadata.
  • Post-processor plugin entry-points, allow for customisable modifications of the notebook, including source code formatting with black

Configuration Examples

Installation

To install from Conda (recommended):

conda install -c conda-forge pytest-notebook

To install via pip from PyPI:

pip install pytest-notebook

To install the development version:

git clone https://github.com/chrisjsewell/pytest-notebook .
cd pytest-notebook
pip install --upgrade pip
pip install -e .
# pip install --group test --group pre_commit -e .[docs] # install development dependencies

Usage

See the documentation at: https://pytest-notebook.readthedocs.io/

If you want to test some sample notebooks, add the --nb-test-files flag:

git clone https://github.com/chrisjsewell/pytest-notebook
cd pytest-notebook/examples
pip install pytest ipykernel pytest-notebook
pytest --nb-test-files *.ipynb

You should see success.ipynb passed the test and fail.ipynb failed the test.

You can regenerate the outputs of fail.ipynb with the --nb-force-regen flag:

pytest --nb-test-files --nb-force-regen *.ipynb

Contributing

Contributions are very welcome.

The following will discover and run all unit test:

pip install --group test -e .
pytest -v

Coding Style Requirements

The code style is tested and formatted using ruff, with the configuration set in pyproject.toml.

Installing the pre_commit dependency group (pip install --group pre_commit -e .) makes the pre-commit package available, which will ensure the code style is met by reformatting the code and testing for lint errors before submitting a commit. It can be setup by:

cd pytest-notebook
pre-commit install

Editors like VS Code also have automatic code reformat utilities, which can adhere to this standard.

License

Distributed under the terms of the BSD-3 license, pytest-notebook is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Acknowledgements

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_notebook-0.11.0.tar.gz (3.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_notebook-0.11.0-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_notebook-0.11.0.tar.gz.

File metadata

  • Download URL: pytest_notebook-0.11.0.tar.gz
  • Upload date:
  • Size: 3.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for pytest_notebook-0.11.0.tar.gz
Algorithm Hash digest
SHA256 008aaa999421174e7945f1a0d8fa7a46bc9d84d961ce23182120be12baa17f8a
MD5 f51debb33801e9e49c7ca166387b098d
BLAKE2b-256 a7e97a675397afeef0e2b40c492124c88d56be4e9ef4987ab806ff198678d649

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_notebook-0.11.0.tar.gz:

Publisher: tests.yml on chrisjsewell/pytest-notebook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_notebook-0.11.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_notebook-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 424418c3d8a34868f75a00e35ce3393b34ed2a0fe72f065d34f817938e896d84
MD5 1b9ad03ba9084122af026c57d7289ca2
BLAKE2b-256 abeead577a3c5a4d8ae894a9b67011e2c9edfd961e7e21dba0d3f2c4cf12e0ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_notebook-0.11.0-py3-none-any.whl:

Publisher: tests.yml on chrisjsewell/pytest-notebook

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page