A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
docs: pytest in marimo (#3281)
Dylan Madisetti011655096ef1
diff --git a/docs/guides/index.md b/docs/guides/index.mdindex 7b80401419..f73e0a91d3 100644--- a/docs/guides/index.md+++ b/docs/guides/index.md@@ -9,7 +9,7 @@ These guides cover marimo's core concepts.| Guide | Description || :--------------------------------------------------- | :----------------------------------------------------------- |-| [Reactive execution](reactivity.md) | Understanding how marimo runs cells |+| [Reactive execution](reactivity.md) | Understanding how marimo runs cells || [Interactive elements](interactivity.md) | Using interactive UI elements || [Visualizing outputs](outputs.md) | Creating markdown, plots, and other visual outputs || [Migrating from Jupyter](coming_from/jupyter.md) | Tips for transitioning from Jupyter |@@ -17,6 +17,7 @@ These guides cover marimo's core concepts.| [Working with data](working_with_data/index.md) | Using SQL cells, no-code dataframe tools, and reactive plots || [Apps](apps.md) | Running notebooks as apps || [Scripts](scripts.md) | Running notebooks as scripts |+| [Tests](testing/index.md) | Running unit tests in notebooks || [Export to HTML and other formats](exporting.md) | Exporting notebooks to HTML and flat scripts || [Run notebooks with WebAssembly](wasm.md) | Create notebooks in our online playground || [Deploying](deploying/index.md) | Deploying marimo notebooks and apps |diff --git a/docs/guides/testing/doctest.md b/docs/guides/testing/doctest.mdnew file mode 100644index 0000000000..effb709d62--- /dev/null+++ b/docs/guides/testing/doctest.md@@ -0,0 +1,12 @@+# Testing with doctest++You can test code snippets in docstrings using+[doctest](https://docs.python.org/3/library/doctest.html), a Python module for+testing code snippets in documentation. This works because marimo notebooks are+just Python programs.++See this notebook for an example:++/// marimo-embed-file+ filepath: examples/testing/running_doctests.py+///diff --git a/docs/guides/testing/index.md b/docs/guides/testing/index.mdnew file mode 100644index 0000000000..0c216a65cc--- /dev/null+++ b/docs/guides/testing/index.md@@ -0,0 +1,9 @@+# Testing notebooks++Because marimo notebooks are stored as Python, test them like any other Python+program.++| Guide | Description |+| --------------------- | ----------------------------------------------------------------------- |+| [pytest](pytest.md) | Include unit tests in notebooks, or implement entire tests as notebooks |+| [doctest](doctest.md) | Test code snippets in docstrings using doctest |diff --git a/docs/guides/testing/pytest.md b/docs/guides/testing/pytest.mdnew file mode 100644index 0000000000..51d20da7c2--- /dev/null+++ b/docs/guides/testing/pytest.md@@ -0,0 +1,94 @@+# Running unit tests with pytest++Since marimo notebooks are Python programs, you can test them using+[`pytest`](https://docs.pytest.org/en/stable/), a popular testing framework+for Python.++++For example,++```bash+pytest test_notebook.py+```++runs and tests all notebook cells whose names start with `test_`.++!!! tip "Naming cells"++ Name a cell by giving its function a name in the notebook file, or using+ the cell action menu in the notebook editor.++!!! note "Use marimo notebooks just like normal pytest tests"++ Include test notebooks (notebooks whose names start with `test_`) in your+ standard test suite, and `pytest` will discover them automatically.+ In addition, you can write self-contained notebooks that contain their own+ unit tests, and run `pytest` on them directly (`pytest my_notebook.py`).++## Example++Running `pytest` on++```python+# content of test_notebook.py+import marimo++__generated_with = "0.10.6"+app = marimo.App()+++def _():+ def inc(x):+ return x + 1+ return inc+++def test_answer(inc):+ assert inc(3) == 5, "This test fails"+++def test_sanity(inc):+ assert inc(3) == 4, "This test passes"+```++prints++```pytest+============================= test session starts ==============================+platform linux -- Python 3.11.10, pytest-8.3.3, pluggy-1.5.0+rootdir: /notebooks+configfile: pyproject.toml+collected 2 items++test_notebook.py F. [100%]++=================================== FAILURES ===================================+__________________________________ test_fails __________________________________++ import marimo++ __generated_with = "0.10.6"+ app = marimo.App(width="medium")+++ @app.cell+ def _():+ def inc(x):+ return x + 1+ return (inc,)+++ @app.cell+ def test_answser(inc):+> assert inc(3) == 5, "This test fails"+E AssertionError: This test fails++test_notebook.py:16: AssertionError+=========================== short test summary info ============================+FAILED test_notebook.py::test_fails - AssertionError: This test fails+========================= 1 failed, 1 passed in 0.20s ===========================+```diff --git a/docs/index.md b/docs/index.mdindex c590e02a38..c23612f74a 100644--- a/docs/index.md+++ b/docs/index.md@@ -59,6 +59,7 @@ reproducibility, maintainability, composability, and shareability.- 🖐️ **interactive:** [bind sliders, tables, plots, and more](guides/interactivity.md) to Python — no callbacks required- 🔬 **reproducible:** [no hidden state](guides/reactivity.md#no-hidden-state), deterministic execution, [built-in package management](guides/editor_features/package_management.md)- 🏃 **executable:** [execute as a Python script](guides/scripts.md), parameterized by CLI args+- 🧪 **testable:** [run your favorite test suite](guides/testing/index.md), verify your notebook's correctness- 🛜 **shareable**: [deploy as an interactive web app](guides/apps.md) or [slides](guides/apps.md#slides-layout), [run in the browser via WASM](guides/wasm.md)- 🛢️ **designed for data**: query dataframes and databases [with SQL](guides/working_with_data/sql.md), filter and search [dataframes](guides/working_with_data/dataframes.md)- 🐍 **git-friendly:** notebooks are stored as `.py` filesdiff --git a/examples/README.md b/examples/README.mdindex 960f96bff8..f682805513 100644--- a/examples/README.md+++ b/examples/README.md@@ -6,10 +6,11 @@ use marimo's features as well as inspire you to make awesome notebooks.- 🖱️ [`ui/`](ui/): how to use UI elements or widgets- 🛢️ [`sql/`](sql/): how to use SQL in marimo-- 🛢️ [`control_flow/`](control_flow/): how to control cell execution and output display-- 🛢️ [`markdown/`](markdown/): how to write markdown, including dynamic markdown+- ⛲ [`control_flow/`](control_flow/): how to control cell execution and output display+- 📝 [`markdown/`](markdown/): how to write markdown, including dynamic markdown- 📽️ [`layouts/`](layouts/): how to present notebooks as slides, add sidebars, and more- 🤖 [`ai/`](ai/): AI-related examples+- 🧪 [`testing/`](testing/): how to test marimo notebooks, and use marimo notebooks as tests- 📦 [`third_party/`](third_party/): using popular third-party packages in marimo- ☁️ [`cloud/`](cloud/): using various cloud providers- 🧩 [`frameworks/`](frameworks/): integrating with different frameworks (web/ASGI)diff --git a/examples/testing/README.md b/examples/testing/README.mdnew file mode 100644index 0000000000..5dd490673e--- /dev/null+++ b/examples/testing/README.md@@ -0,0 +1,11 @@+# Testing 🧪++These basic examples show how to use test marimo notebooks.++## Testing with pytest++Run `pytest test_with_pytest.py`.++## Testing with doctests++See `running_doctests.py`diff --git a/examples/testing/running_doctests.py b/examples/testing/running_doctests.pynew file mode 100644index 0000000000..979dac391a--- /dev/null+++ b/examples/testing/running_doctests.py@@ -0,0 +1,48 @@+import marimo++__generated_with = "0.10.6"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ def euclid_mcd(a: int, b: int) -> int:+ """Return the MCD between positive a, b.+ >>> euclid_mcd(42, 24)+ 6+ >>> euclid_mcd(24, 42)+ 6+ >>> euclid_mcd(42, 42)+ 42+ """+ assert a > 0+ assert b > 0+ if a < b:+ a, b = b, a+ if (a != b):+ r = a - b+ return euclid_mcd(b, r)+ return a+ return (euclid_mcd,)+++def _(euclid_mcd, mo):+ # Include a reference to each function to test+ euclid_mcd++ import doctest++ failures, success = doctest.testmod(verbose=True)+ mo.md(f"Success: {success}, Failures: {failures}")+ return doctest, failures, success+++if __name__ == "__main__":+ app.run()diff --git a/examples/testing/test_with_pytest.py b/examples/testing/test_with_pytest.pynew file mode 100644index 0000000000..a8bad13601--- /dev/null+++ b/examples/testing/test_with_pytest.py@@ -0,0 +1,24 @@+import marimo++__generated_with = "0.10.6"+app = marimo.App()+++def _():+ def inc(x):+ return x + 1+ return inc+++def test_answer(inc):+ assert inc(3) == 5, "This test fails"+++def test_sanity(inc):+ assert inc(3) == 4, "This test passes"++if __name__ == "__main__":+ app.run()diff --git a/mkdocs.yml b/mkdocs.ymlindex 9467f8d4d6..a2cc76fba4 100644--- a/mkdocs.yml+++ b/mkdocs.yml@@ -86,6 +86,10 @@ nav:- Plotting: guides/working_with_data/plotting.md- Run notebooks as apps: guides/apps.md- Run notebooks as scripts: guides/scripts.md+ - Testing notebooks:+ - Overview: guides/testing/index.md+ - pytest: guides/testing/pytest.md+ - doctest: guides/testing/doctest.md- Export to HTML and other formats: guides/exporting.md- Run notebooks with WebAssembly: guides/wasm.md- Deploy notebooks: