Skip to content

Commit

Permalink
Add tests for the code blocks in Markdown files (courtesy of @tlambert03
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aliddell committed Aug 2, 2024
1 parent 8610f05 commit d621eff
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Testing the Documentation

To test the documentation, you will need to have several packages installed in your Python environment.
You can install them with the following command:

```bash
python -m pip install -r requirements-test.txt
```

(You can find the `requirements-test.txt` file in the folder containing this README file.)

Then execute the tests with the following command:

```bash
python -m pytest
```
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
acquire-imaging
napari[all]
numpy
ome-zarr
pytest
tifffile
zarr
35 changes: 35 additions & 0 deletions tests/test_tutorials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import atexit
from pathlib import Path
import shutil
import subprocess
import re
import logging
import tempfile
import pytest


logging.getLogger("acquire").setLevel(logging.CRITICAL)

CODE_BLOCK = re.compile(r"```python\n(.*?)```", re.DOTALL)
SKIP = {
"setup.md", # has invalid syntax
"trigger.md", # has some non-existant paths
}


def tutorials():
docs_path = Path(__file__).parent.parent / "docs"

tuts = []
if (get_started := docs_path / "get_started.md").exists():
tuts.append(get_started)
tuts.extend([fn for fn in docs_path.glob("tutorials/*.md") if fn.name not in SKIP])
tuts.sort()

return tuts


@pytest.mark.parametrize("tutorial", tutorials(), ids=lambda x: x.name)
def test_tutorials(tutorial: Path):
for code_block in CODE_BLOCK.finditer(tutorial.read_text()):
exec(code_block.group(1))

0 comments on commit d621eff

Please sign in to comment.