Unit testing for Jupyter (iPython) notebooks

· 217 words · 2 minute read

At Quantego, we do most high-level work that supports energy analysts in Jupyter Notebooks. This allows us to pull several Java and Python packages together for a highly productive work environment.

Sample notebooks are hosted on Github and distributed with our Docker images. Of course we prefer for our sample notebooks to work, when people run them. They also uncover potential problems, by running at a very high level and thus using almost all available features.

If you have a similar setup – for example in a data analytics-driven environment – the following could work for you as well:

  1. Make sure your notebooks run correctly when running “Run All”. When testing you may try different things and run cells out of order. For automatic testing to work, they should run all in sequence.
  2. Test locally with jupyter nbconvert –to=html –ExecutePreprocessor.enabled=True my-notebook.ipynb
This will convert your notebooks to HTML. We're not intersted in the output, only potential errors during conversion. This only works with Jupyter and iPython >=4. Previous versions simply ignore errors.</li> 

  * Next you could just run the same command in an isolated Docker container or in a CI step <pre class="">docker run my_container /bin/sh -c \

“/usr/local/bin/jupyter nbconvert
–to=html –ExecutePreprocessor.enabled=True
–ExecutePreprocessor.timeout=3600
samples/my-sample.ipynb”

    A [full working example][4] for [CircleCI][5] can be found in our [sample-repo][2].</li> </ol>