Registry / testing / testbook

testbook

JSON →
library0.4.2pypypi✓ verified 86d ago

A unit testing framework for Jupyter Notebooks. Current version 0.4.2 (release cadence: infrequent, last release Sep 2021). Allows writing and running tests for notebook cells using Python test frameworks.

pip install testbook
INSTALL
IMPORT
SIG · TESTBOOK
T
testbook
testingpythonv0.4.2
Install
4.0s avg
Import
902ms
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.950s · 36.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.0s · import 0.854s · 33MB
33MB installed
● package 33MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

testbook
from testbook import testbook
import testbook
Importing the module directly gives you the package, not the decorator/context manager. The main API is the testbook function/decorator.

Decorate a test function with @testbook, specifying the notebook path. The test function receives a Testbook object. Calling tb.cell_output(index) retrieves the output of a cell.

from testbook import testbook @testbook('notebook.ipynb', execute=True) def test_something(tb): result = tb.cell_output(0) assert result == 42
Debug
Known issues
gotchaWhen using execute=True (default), the entire notebook is executed before test. This may be slow and side-effect heavy. Use execute=False for partial execution.
fix
Set execute=False in the decorator: @testbook('notebook.ipynb', execute=False), then run tb.execute_cell(0) to run specific cells in your test.
affects: all
gotchaCell indices are 0-based, but non-code cells (e.g. markdown) are skipped. Counting cells manually can be error-prone.
fix
Use tb.cell_output_slice() or iterate with tb.cells to find the correct index. Alternatively, use cell tags in the notebook to reference cells by name.
affects: all
deprecatedThe old API function 'testbook.testbook' (lowercase) has been deprecated in favor of the decorator usage.
fix
Update imports: from testbook import testbook. Use @testbook decorator or context manager.
affects: <0.4.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'testbook'
testbook is not installed or installed in a different environment.
fix
Install testbook: pip install testbook
AttributeError: module 'testbook' has no attribute 'testbook'
Old import style (import testbook then testbook.testbook) no longer works in recent versions.
fix
Use: from testbook import testbook
FileNotFoundError: [Errno 2] No such file or directory: 'notebook.ipynb'
The notebook path is relative and may not be correct when running tests from a different directory.
fix
Use an absolute path or set the working directory to the notebook's location.
Upgrade
Version history
0.4.2latest on PyPI · released Jun 2, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
10 hits · last 30 days
node
10
Resources