nbmake

1.5.5 · active · verified Tue Apr 14

nbmake is a pytest plugin designed for testing Jupyter notebooks. It executes notebooks programmatically, allowing for continuous integration and automated quality checks on documentation and research materials. Currently at version 1.5.5, the library maintains an active development cadence, frequently releasing updates to support new Python and Pytest versions and address user feedback.

Warnings

Install

Imports

Quickstart

To quickly test a Jupyter notebook, install `nbmake` and `pytest`, then invoke pytest with the `--nbmake` flag on your notebook file(s). nbmake will execute the notebook from top to bottom, failing if any cell raises an unhandled exception or an assertion fails. Wildcards can be used to test multiple notebooks.

import os

# Create a dummy notebook for testing
notebook_content = """
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 1\n",
    "y = 2\n",
    "assert x + y == 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Notebook executed successfully')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
"""

with open("test_notebook.ipynb", "w") as f:
    f.write(notebook_content)

# To run this, save it as a .py file and execute from your terminal:
# python -c "$(cat your_script.py)" # (or simply run the file)
# Then, in the same directory:
# pytest --nbmake test_notebook.ipynb

print("Created test_notebook.ipynb. Run 'pytest --nbmake test_notebook.ipynb' in your terminal.")

view raw JSON →