Skip to main content

Pytest plugin to run tests in Jupyter Notebooks

Project description

pytest-ipynb2

A pytest plugin to run tests in Jupyter Notebooks.

Designed to play nicely with chmp/ipytest.

Why?

My use case is so that I can teach my son to code and use notebooks to do that but still have the tests show up in vscode test explorer.

We also like to have all our tests run in a github workflow - and this makes that simple too.

Usage

Usage is very simple:

  1. Install from pypi (e.g. with pip):

    pip install pytest-ipynb2
    
  2. Enable by adding to the default options in pyproject.toml:

    [tool.pytest.ini_options]
      addopts = [
        "-p pytest_ipynb2.plugin",
      ]
    
  3. That's it! pytest will now collect and execute any tests in jupyter notebooks when run form the command line or IDE.

Test identification

I'm assuming you also want to run your tests inside your notebooks ... so simply use the %%ipytest magic in a cell and pytest will collect any tests based on the usual naming and identification rules.

Note: tests will only be identified in cells which use the %%ipytest magic

Documentation

For more details see the docs

Features

  • Enables pytest to collect and execute tests stored within jupyter notebooks

  • Provides meaningful test logs identifying the notebook, cell and test function

  • Handles tests with fixtures and parametrization

  • Executes all cells above the test cell before running the tests in that cell.

    WARNING: this means that if any previous cells have side-effects they will occur on test collection, just as they would if included in a pytest test module.

Known limitations & To-Dos

This is an early version. The following things are still on my to-do list:

  • Handling tests grouped into classes #22 (might work - I've not checked yet)
  • Assertion re-writing. Failed tests don't yet give the expected quality of pytest output about the failure #20 (Workaround - re-run the test using ipytest inside the notebook)
  • v1.0.0 will include dedicated commandline options rather than requiring you to specify the plugin #12
  • This won't play nicely with other magics or direct calls to ipytest functions yet #23

Project details


Download files

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

Source Distribution

pytest_ipynb2-0.3.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

pytest_ipynb2-0.3.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_ipynb2-0.3.0.tar.gz.

File metadata

  • Download URL: pytest_ipynb2-0.3.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_ipynb2-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f3168452e0da90f31561634422398fcbc9cff76821dfbf2dab5634802ae0c3de
MD5 377f82708677eec510ae88f0bd9c0025
BLAKE2b-256 d9ed1f9c8ac8c77f894d676b7eec3e89d94034e4d81c54bf24338d688e3523cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_ipynb2-0.3.0.tar.gz:

Publisher: publish-python.yml on MusicalNinjaDad/pytest-ipynb2

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_ipynb2-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_ipynb2-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_ipynb2-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eabbd69a2a4efb51875d00fe08d4cdf8b771c5929664263879a2867ebaee981c
MD5 f274b00e3f52a199c227cdede7177646
BLAKE2b-256 d4512730c0e4412329667f8fae914922fa217c7944398b3aa61d37c2afb39800

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_ipynb2-0.3.0-py3-none-any.whl:

Publisher: publish-python.yml on MusicalNinjaDad/pytest-ipynb2

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 Pingdom Monitoring Sentry Error logging StatusPage Status page