Upgrade to Pro — share decks privately, control downloads, hide ads and more …

testbook - Unit test your Jupyter Notebooks

testbook - Unit test your Jupyter Notebooks

With widespread use of Jupyter Notebooks, it becomes important to write maintainable and reliable notebooks. testbook helps you unit test your notebooks in the conventional unit testing style, with the tests existing as a separate entity.


Rohit Sanjay

July 07, 2020

More Decks by Rohit Sanjay

Other Decks in Programming


  1. testbook Unit test your Jupyter Notebooks the right way SciPy

  2. About me My name is Rohit Sanjay - Final year

    electronics engineering student - Google Summer of Code 2020 student working on testbook under the mentorship of Matthew Seal (@codeseal) - Love Python and C github.com/rohitsanj @imrohitsanj rohitsanjay.com
  3. Introduction Context behind why testbook was created

  4. Jupyter Notebooks - going beyond experimentation Jupyter Notebooks are no

    more just a tool for experimentation, but are often being using in production environments.
  5. Jupyter Notebooks can get very messy Code written to conduct

    data science experiments in Jupyter Notebooks can get messy. Enforcing good coding habits in Jupyter Notebooks can lead to maintainable and easily refactorable code. Some good habits are.. • Use functions to abstract away complexity • Smuggle code out of Jupyter notebooks as soon as possible • Apply test driven development • Make small and frequent commits Source: https://www.thoughtworks.com/insights/blog/coding-habits-data-scientists
  6. Test driven development for Jupyter Notebooks? Couple of approaches: •

    Write the tests in the notebook itself • Refactor code out of the notebooks and write them in separate Python modules that can then be independently unit tested.
  7. Source: https://www.thoughtworks.com/insights/blog/coding-habits-data-scientists

  8. This is where testbook comes in You can write unit

    tests directly for Jupyter Notebooks, without refactoring code out of it into separate Python modules. Testbook can now help you write maintainable and reliable Jupyter Notebooks.
  9. Features • Write conventional unit tests for Jupyter Notebooks •

    Execute all or some specific cells before unit test • Share kernel context across multiple tests (using pytest fixtures) • Inject code into Jupyter notebooks • Works with any unit testing library - pytest, unittest or nose
  10. A simple unit test using testbook

  11. Another example

  12. Thanks! PyPI - pypi.org/project/nteract-testbook pip install nteract-testbook GitHub - github.com/nteract/testbook

    (drop a star for good karma) Docs - test-book.readthedocs.io