How To Use

%load_ext testmynb

Load testmynb to the ipython kernel to start using testmynb.

In [1]:
%load_ext testmynb

%%testcell Magic

By loading testmynb to the ipython kernel, the %%testcell magic becomes available.

By adding the %%testcell magic to the cell, it flags the cell to be executed as a test by the testmynb command line tool.

In [2]:
%%testcell example_test_cell

assert 1==1, 'Example assert statement'

Test Cell

Test cells should generally have two things: a title and an assert statement.

Cell Title
The first positional argument of the cell magic line is used as the test cell’s title.

When running testmynb, the failed/errored test cell’s title is displayed to allow the user to distinguish which test failed/errored. If no title is given, the title falls back to unnamed.

You may still distinguish the failed/errored test cell by observing the cell body and the traceback of the test.

Assert Statement
Like pytest, a test cell should contain an assert statement to test whatever you’re testing.

If any of the two above are missing, a message is displayed when the cell is executed.

In [3]:
%%testcell
test = True
# No assert statement nor cell title.
# Running this cell will print out a message
# saying there is no assert statement nor a cell title.
[testmynb] Assert statement missing.
[testmynb] Cell title missing.

-n option

The -n option is used to run the cell during the test, but not to treat it as a test. This can be used for cells which contain all the required import statements, or as a setup/teardown cell.

In [4]:
%%testcell import_statements -n
import os
import sys

testmynb commandline

Run the testmynb command, and the command line tool searches for all the .ipynb files with the test_ prefix and runs the designated test cells.

In [5]:
!testmynb
========================== Test My Notebook (0.0.1) ==========================
Platform darwin
Python 3.7.1
Working Directory: ${PWD}

7 test cells across 2 notebook(s) detected.
Notebooks:
Trusted test_how_to.ipynb: ...
Untrusted test_why_use_testmynb.ipynb: ....

============== 7 test(s) passed, 0 failed, and 0 raised an error ==============