Ipynbtester - IPython Notebook Test Suite

Forged from runtests (Matt Davis) gist: https://gist.github.com/jiffyclub/4013594

Usage on notebooks

1. Load IPython Notebook tester (ipynbtester) extension and runaway function


In [1]:
%load_ext ipynbtester


The ipynbtester module is not an IPython extension.

While loading extension, IPython notebook informs that module is not part of the core distribution, but third party module instead.

2. Define tests

Tests must be defined as function starting with test -keyword. Underscore is not necessary after the keyword, but good for readability. Below three different tests are defined, first indicating successful test assertion, second having failure on assertion and third having error during script execution.


In [2]:
def test_my_test_ok():
    assert True == True

def test_my_test_fail():
    assert True == False
    
def test_my_test_error():
    assert True == none

3. Run tests

Runaway "magic" -function runs all test functions starting with test -keyword and outputs a table with collected data. Red colors and separate fail and error parts on a table body indicates results of the tests.


In [3]:
%runaway


Out[3]:
Collected 3 tests.
Test function nameStatus
Successful1
Failed1
Errors1
Execution0.0009999 seconds
test_my_test_failfailed
test_my_test_errorerror
test_my_test_oksuccessful
Failed
test_my_test_failAssertionError()
Errors
test_my_test_errorNameError("global name 'none' is not defined",)

Styles below are just for nice look for result table, not necessary for ipynbtester functionality itself.


In [4]:
from IPython.core.display import HTML
with open('styles.css') as f:
    css = f.read()
HTML('<style>%s</style>' % css)


Out[4]:

The MIT License

Copyright (c) 2014 Marko Manninen