Skip to main content

Pytest plugin for testing Jupyter Notebooks

Project description

nbtest

A pytest plugin for testing Jupyter Notebooks

The nbtest-plugin adds the functionality to define tests (assertions) in Jupyter Notebooks for testing key properties. These tests are later collected by pytest, when used with the --nbtest flag

Assertions supported

Generic assertions

  • assert_equal
  • assert_allclose
  • assert_true
  • assert_false

DataFrame-specific assertions

  • assert_nanvar: Test the variance of the dataframe
  • assert_nanmean: Test the mean of the dataframe
  • assert_column_types: Test the data type of each column in the dataframe
  • assert_column_names: Test the column names of each column in the dataframe

Usage example

Testing
import nbtest
import math
import numpy as np

nbtest.assert_equal(round(math.pi, 2), 3.14)
nbtest.assert_true(math.pi == 3)

print(f'PI: {math.pi}')

These tests do not report any errors when the notebook is executed

$ jupyter execute example.ipynb --output=run.ipynb
[NbClientApp] Executing example.ipynb
[NbClientApp] Executing notebook with kernel:
[NbClientApp] Save executed results to run.ipynb

And output of the cell is:

PI: 3.141592653589793

Now, we execute the tests using pytest

$ pytest -v --nbtest example/nbtest_plugin_example.ipynb
==================================================== test session starts =====================================================
platform linux -- Python 3.11.11, pytest-8.3.4, pluggy-1.5.0 -- /home/verve/miniconda3/envs/test/bin/python
cachedir: .pytest_cache
rootdir: /home/verve/ml_nb_testing/comp_analysis/nbtest-release
plugins: nbtest-0.1.0, anyio-4.6.2
collected 2 items

example/nbtest_plugin_example.ipynb::nbtest_id_0_5 PASSED                                                              [ 50%]
example/nbtest_plugin_example.ipynb::nbtest_id_0_6 FAILED                                                              [100%]

========================================================== FAILURES ==========================================================
________________________________________ example/nbtest_plugin_example.ipynb::Cell 0 _________________________________________
Assertion failed
Cell 0: Assertion error

Input:
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[1], line 1
----> 1 nbtest.tc.assertTrue(math.pi == 3)

File ~/miniconda3/envs/test/lib/python3.11/unittest/case.py:715, in TestCase.assertTrue(self, expr, msg)
    713 if not expr:
    714     msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
--> 715     raise self.failureException(msg)

AssertionError: False is not true

================================================== short test summary info ===================================================
FAILED example/nbtest_plugin_example.ipynb::nbtest_id_0_6
================================================ 1 failed, 1 passed in 1.34s =================================================

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

nbtest_plugin-0.1.4.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

nbtest_plugin-0.1.4-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nbtest_plugin-0.1.4.tar.gz.

File metadata

  • Download URL: nbtest_plugin-0.1.4.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for nbtest_plugin-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1aaeec7662447f8de9d3e9c78a25b9a03e051a8139df7d96477f87b9bb0c9762
MD5 ccb6381e1d411e1b8402f2a88a1a6a76
BLAKE2b-256 ebbec13e9b79e3bbfa0009321b0e32afad37f0deef8d570b741fc782f90d7ec2

See more details on using hashes here.

File details

Details for the file nbtest_plugin-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: nbtest_plugin-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for nbtest_plugin-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 75b4d7fc6753f6bbd95652b34d68d192bb7ceff5e02983fc21c12e7b774f0a8f
MD5 62d2a0784766229de2cc8d716a4a5290
BLAKE2b-256 b36b9822b2c621e64f1c2bd53832309b08607ad332dd8009bbafedcc3f0ab19a

See more details on using hashes here.

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