pytest plugin to test code sections in your documentation.
pip install pytest_docfiles
Define code sections in your markdown files.
<!-- doc.md -->
# Hello World
```python
print("hello world!")
```
run pytest on your markdown files with the --docfiles
flag.
pytest --docfiles doc.md
Define names for your code sections so they can be better identified in your pytest output
<!-- doc.md -->
```python {"name": "my-section"}
print("hello world")
```
$ pytest --docfiles doc.md
...
doc.md::my-section PASSED
...
Define your fixtures in conftest.py
as usual
# conftest.py
import pytest
@pytest.fixture
def custom_fixture() -> str:
return "fixture value"
@pytest.fixture(autouse=True)
def autouse() -> None:
"""autouse fixtures are used in each code section"""
use the fixtures in your code sections
<!-- doc.md -->
```python {"fixtures": ["custom_fixture"]}
assert custom_fixture == "fixture value"
```
Code section depending on other code section can be executed in scopes.
```python {"scope": "my-scope"}
value = True
```
```python {"scope": "my-scope"}
assert value is True
```
```python {"skip": true}
raise Exception("this section should not run")
```
```python {"raises": "RuntimeError"}
raise RuntimeError("this section should pass")
```