Notebook Testing
NVIDIA FLARE uses nbmake to test Jupyter notebooks. This ensures that example notebooks remain functional as the codebase evolves.
Note
Not all notebooks are ready for automated testing yet. Some notebooks require external infrastructure (running FLARE servers, provisioned environments, specific datasets) or contain interactive elements that cannot run in CI. Notebook test coverage is being improved over time.
For general runtest.sh usage (dependency caching, verbose mode, etc.), see Developer Testing.
Quick Start
Use the runtest.sh script to run notebook tests:
# Test default notebook (flare_simulator.ipynb)
./runtest.sh -n
# Test a specific notebook
./runtest.sh -n examples/tutorials/flare_simulator.ipynb
# Test with verbose output
./runtest.sh -n -v examples/tutorials/flare_simulator.ipynb
Notebook-Specific Options
These options are specific to notebook testing (-n):
Argument |
Default |
Description |
|---|---|---|
|
1200 |
Timeout in seconds for each notebook execution |
|
on-success |
When to clean outputs: |
|
python3 |
Jupyter kernel name (defaults to |
|
off |
Pass |
Examples
# Set a shorter timeout (5 minutes)
./runtest.sh -n --timeout=300 examples/tutorials/flare_simulator.ipynb
# Use a specific kernel
./runtest.sh -n --kernel=python3 examples/tutorials/flare_simulator.ipynb
# Always clean outputs regardless of pass/fail
./runtest.sh -n --nb-clean=always examples/tutorials/
# Combine multiple options with verbose output
./runtest.sh -n -v --timeout=1800 --kernel=python3 examples/tutorials/
Direct pytest Usage
You can also run nbmake directly with pytest:
pytest --nbmake --nbmake-timeout=1200 --nbmake-clean=on-success examples/tutorials/
# With specific kernel
pytest --nbmake --nbmake-timeout=1200 --kernel=python3 examples/tutorials/
Skipping Cells in Notebooks
To skip specific cells during automated testing (e.g., Colab setup cells, interactive visualizations, or cells that require user input), add one of these tags to the cell metadata:
skip-executionskipcolab
How It Works
The testing framework (implemented in conftest.py) automatically:
Before test: Creates a
.backupof the original notebookFilters cells: Removes cells tagged with
skip-execution,skip, orcolabUpdates kernel: Adjusts kernel spec to match the specified or detected kernel
Executes: nbmake runs the notebook through the Jupyter kernel
Restores: Original notebook is restored from backup
Cleans outputs: Cell outputs are cleared based on
--nbmake-cleansetting
This ensures:
The test does not modify the committed notebook (original is restored from backup)
Notebooks can contain Colab-specific or interactive cells that won’t break CI
Consistent kernel usage across different development environments
Troubleshooting
Kernel Not Found
If you see “Kernel not found” errors:
Ensure your virtual environment is activated
Install ipykernel:
pip install ipykernelRegister your kernel:
python -m ipykernel install --user --name=my_envOr specify an existing kernel:
./runtest.sh -n --kernel=python3
Timeout Errors
For long-running notebooks, increase the timeout:
./runtest.sh -n --timeout=3600 examples/advanced/
Notebook Requires External Infrastructure
Some notebooks (e.g., flare_api.ipynb) require a running FLARE server or provisioned
environment. These notebooks will fail in automated testing unless the infrastructure is set up.
For such notebooks, consider:
Running them manually in an interactive environment
Adding
skip-executiontags to cells that require external servicesCreating a simplified version for automated testing
Best Practices
Tag cells appropriately: Mark Colab setup, interactive widgets, and user-input cells with
skip-executionKeep notebooks focused: Smaller notebooks are faster to test and easier to debug
Use reasonable timeouts: Increase
--timeoutbased on expected execution time plus bufferTest locally before pushing: Run
./runtest.sh -non your notebooks before committingClean outputs before committing: Use
--nb-clean=alwaysor manually clear outputs to keep git diffs cleanUse self-contained examples: Notebooks that use the simulator (like
flare_simulator.ipynb) are easier to test than those requiring external servers