Welcome to testbook¶
testbook is a unit testing framework for testing code in Jupyter Notebooks.
Previous attempts at unit testing notebooks involved writing the tests in the notebook itself. However, testbook will allow for unit tests to be run against notebooks in separate test files, hence treating .ipynb
files as .py
files.
Here is an example of a unit test written using testbook
Consider the following code cell in a Jupyter Notebook:
def func(a, b):
return a + b
You would write a unit test using testbook
in a Python file as follows:
from testbook import testbook
@testbook('/path/to/notebook.ipynb', execute=True)
def test_func(tb):
func = tb.ref("func")
assert func(1, 2) == 3
Features¶
Write conventional unit tests for Jupyter Notebooks
Share kernel context across multiple tests (using pytest fixtures)
Inject code into Jupyter notebooks
Works with any unit testing library - unittest, pytest or nose