pytest-markdown-docs

0.9.2 · active · verified Wed Apr 15

pytest-markdown-docs is a pytest plugin that collects and executes Python code blocks found within Markdown files and Python docstrings as tests. It allows developers to ensure that documentation examples remain correct and up-to-date by integrating them into the standard pytest test suite. The library is currently at version 0.9.2 and follows an active, though not strictly fixed, release cadence.

Warnings

Install

Quickstart

To use pytest-markdown-docs, install it and then simply invoke pytest with the `--markdown-docs` flag, pointing it to your Markdown files or Python modules containing docstrings. The plugin will discover and run code fences tagged as `python`, `python3`, or `py`.

import pytest

# Save this content as README.md
readme_content = """
# My Project

This is a sample project with a Python code example.

```python
def add(a, b):
    return a + b

assert add(1, 2) == 3
assert add(-1, 1) == 0
```

Another block that should be skipped:
```python notest
print('This code should not run as a test')
assert False # This would fail if not skipped
```
"""

with open('README.md', 'w') as f:
    f.write(readme_content)

# To run this, save the above content as a Python file (e.g., quickstart.py),
# then execute from your terminal:
# python -c "import quickstart" # Creates README.md
# pytest --markdown-docs README.md
#
# Expected output will show 2 passed tests from the first code block.

view raw JSON →