Skip to main content

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 =================================================

Metadata

Release files for nbtest-plugin 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbtest-plugin 0.1.6
File Size Uploaded
nbtest_plugin-0.1.6.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nbtest-plugin 0.1.6
File Interpreter ABI Platform
nbtest_plugin-0.1.6-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 25.7 kB

Release files / nbtest_plugin-0.1.6.tar.gz

Download URL nbtest_plugin-0.1.6.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c462cddd3c7066626c2f94ff6f551119e54a3bb7be365774182f408bd3d4fa51
BLAKE2b-256 checksum
How to use checksums
ac2f9e342b7e015ba7cab583b8118983cc2d6a50d781fea5f1125a780e2459fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / nbtest_plugin-0.1.6-py2.py3-none-any.whl

Download URL nbtest_plugin-0.1.6-py2.py3-none-any.whl
Size 13.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
dbe7ec85ff92bb3fbb98f31cc56597dad51d50d57ba53d9199c8b36556e75ec4
BLAKE2b-256 checksum
How to use checksums
10f9ee131483299296c6d19ccfa7924ce815dd540e308976dfc8f595764d23a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page