How to Test Jupyter Notebooks with Pytest and Nbmake
Blog post from Semaphore
Jupyter notebook files have become increasingly popular on GitHub due to their user-friendly interface for data analysis and code-heavy tasks, yet they often accumulate in broken states, complicating reproducibility and understanding. To address this, the tutorial introduces nbmake, a pytest plugin that automates end-to-end testing of notebooks, enhancing workflow efficiency by reducing manual testing. The guide emphasizes setting up a Python 3 environment and the utility of nbmake, which is widely used by scientific organizations, to test notebooks locally and in continuous integration environments like Semaphore CI. It provides strategies for handling errors, optimizing test execution with pytest-xdist, and persisting test outcomes, while also acknowledging the challenges of testing complex projects with user inputs or long execution times. By advocating for pragmatic solutions, such as ignoring difficult-to-test notebooks and integrating tools like pre-commit and jupyter book, the guide aims to improve the reproducibility and maintainability of Jupyter notebooks in software projects.