Testing marimo Notebooks
marimo notebooks are pure Python, making them fully testable with standard Python testing frameworks. marimo provides first-class pytest integration with reactive test execution.Quick Start
Create a notebook with test functions:Reactive Test Execution
marimo can automatically run pytest on cells containing test functions:- Cells with only test functions/classes are automatically tested
- Test results appear inline in the notebook
- Tests re-run when dependencies change
Writing Tests
Basic Test Functions
Testing Functions Defined in Notebooks
Use@app.function to define testable functions:
Parametrized Tests
Test Classes
Running Tests
In the marimo Editor
Withreactive_tests: True, test cells automatically execute and display results:
Using pytest CLI
Run tests using pytest directly:Programmatic Test Execution
Frommarimo/_runtime/pytest.py, you can run tests programmatically:
Test Fixtures
Cell-Level Fixtures
Define fixtures in the same cell as tests:Shared Fixtures
Define fixtures in a separate cell for reuse:Integration Testing
Testing Data Pipelines
Testing UI Components
Testing Best Practices
Isolate test cells
Isolate test cells
Keep test cells independent and focused:
Use descriptive test names
Use descriptive test names
Follow pytest conventions for test naming:
Test edge cases
Test edge cases
Cover boundary conditions and error cases:
Use fixtures for setup/teardown
Use fixtures for setup/teardown
Avoid duplication with pytest fixtures:
Running Doctests
marimo supports running doctests in notebook cells:Test Configuration
Configure pytest behavior inpyproject.toml:
CI/CD Integration
Run notebook tests in continuous integration:Example: Complete Test Suite
Fromexamples/testing/test_with_pytest.py:
Debugging Failed Tests
When tests fail, marimo provides detailed tracebacks:Related Resources
- pytest Documentation - Complete pytest reference
- marimo Reactivity - Understanding reactive execution
- marimo Functions - Defining reusable functions