[TIP] pytest and ipynb

Pete Forman petef4+usenet at gmail.com
Thu Jun 16 12:32:15 PDT 2016

Hans Fangohr <fangohr.hans at gmail.com> writes:

>> On 13 Jun 2016, at 20:02, Pete Forman <petef4+usenet at gmail.com> wrote:
>> In TDD I write test_foo.py so that I can verify foo.py for validity
>> and coverage. Recently I have been using Jupyter / IPython where the
>> code and docs are in an ipynb notebook.
>> What is the recommended way to run pytest on a test_foo.ipynb?
>> I've found a couple of solutions but to slightly different questions.
>> https://pypi.python.org/pypi/pytest-ipynb enables you to write tests
>> in an ipynb. That enables you to build tests into a notebook but does
>> not separate the tests from the product.
>> https://github.com/computationalmodelling/nbval is a regression
>> testing tool for Jupyter. It compares outputs with a previous run.
>> Am I asking the right question? What is best practice for testing
>> ipynb?

> The nbval tool re-executes a saved notebook, and then (i) reports if
> that execution raises an error of some type and (ii) compares any
> output from that execution with the output stored in the notebook, and
> reports a fail if the output deviates.
> As Pete correctly identifies: nbval is like a regression testing tool
> for Jupyter. It compares outputs with a previous run.
> I hope the above is useful; best wishes,
> Hans

Thanks, Hans. Apologies up front if I have pruned too much of your
helpful reply and converted it to bottom posting. (I am reading via

nbval provides a good way to test notebooks but my last questions still
stand. Is TDD fundamentally a bad fit for Jupyter or is just that no-one
has written a suitable implementation. If the latter then I might step
up to the mark.
Pete Forman

More information about the testing-in-python mailing list