[Paper Review] Testing with Jupyter notebooks: NoteBook VALidation (nbval) plug-in for pytest
This paper introduces nbval, a pytest plugin that validates Jupyter notebook outputs by re-executing cells and comparing results against saved outputs, ensuring reproducibility and test coverage. It enables notebooks to serve as executable documentation, unit, integration, and system tests, with support for output sanitization, error handling, and rich diffing via nbdime.
The Notebook validation tool nbval allows to load and execute Python code from a Jupyter notebook file. While computing outputs from the cells in the notebook, these outputs are compared with the outputs saved in the notebook file, treating each cell as a test. Deviations are reported as test failures, with various configuration options available to control the behaviour. Application use cases include the validation of notebook-based documentation, tutorials and textbooks, as well as the use of notebooks as additional unit, integration and system tests for the libraries that are used in the notebook. Nbval is implemented as a plugin for the pytest testing software.
Motivation & Objective
- To address the challenge of notebook reproducibility over time as underlying libraries evolve.
- To enable Jupyter notebooks to function as executable tests for code, documentation, and tutorials.
- To reduce manual validation overhead by automating output comparison in a CI/CD pipeline.
- To support safe refactoring of code by detecting unintended output changes.
- To provide a lightweight, plugin-based solution integrated with pytest for seamless testing workflows.
Proposed method
- nbval loads and executes each cell in a Jupyter notebook using the IPython kernel.
- It compares the re-computed output of each cell with the output previously saved in the notebook file.
- The tool operates in strict or relaxed mode, with configurable behavior via cell metadata tags such as nbval-check-output and nbval-ignore-output.
- It supports output sanitization using regular expressions to ignore variable content like timestamps or memory addresses.
- It integrates with nbdime to visualize rich diffs, including image and multimedia output comparisons.
- It allows skipping cells (nbval-skip) or ignoring exceptions (nbval-raises-exception) during validation.
Experimental results
Research questions
- RQ1Can Jupyter notebooks be effectively used as a form of automated testing for scientific and data science code?
- RQ2How can output reproducibility in notebooks be validated automatically over time as dependencies change?
- RQ3What mechanisms enable reliable comparison of variable outputs (e.g., timestamps, memory addresses) in notebook outputs?
- RQ4How can notebook validation be integrated into continuous integration pipelines for production use?
- RQ5To what extent can notebooks serve as both documentation and test suites without increasing maintenance overhead?
Key findings
- nbval successfully detects output mismatches in notebooks when code changes cause deviations from previously saved outputs.
- The use of regular expression-based sanitization allows reliable comparison of outputs containing time- or environment-specific values.
- Integration with nbdime enables visual comparison of image and multimedia outputs, improving debugging of differences.
- Notebooks validated with nbval can serve as system tests, increasing test coverage without additional test code.
- The plugin supports incremental test and documentation maintenance, reducing technical debt in research software projects.
- nbval enables early and continuous validation of notebooks, helping prevent regression and ensuring long-term reproducibility.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.