pytest-markdown-docs
Run markdown code fences through pytest
What it is and what it does
pytest-markdown-docs is a pytest plugin that treats Python code blocks in markdown files and docstrings as executable test cases. It scans for fenced code blocks (triple backticks) marked with python, python3, or py language tags, and runs them through pytest's test framework. This bridges the gap between documentation and testing: code examples in your README, API docs, or docstrings can be automatically validated to ensure they remain correct as your codebase evolves.
The plugin supports several advanced features: skipping blocks with notest markers, injecting global variables or fixtures into code blocks, using pytest fixtures (including async fixtures from pytest-asyncio), chaining snippets with continuation markers to maintain state across blocks, and retrying flaky tests. It also handles MDX files with metadata comments and supports custom markdown-it-py parser configurations for compatibility with tools like Material for MkDocs.
Use it for:
- Validate code examples in README files and API documentation to prevent outdated or broken examples.
- Test docstring examples in library code without duplicating assertions in separate test files.
- Ensure tutorial markdown files contain working, up-to-date code snippets that readers can trust.
- Catch regressions in documented usage patterns when refactoring library APIs.
- Generate living documentation where code examples are guaranteed to execute correctly.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that executes Python code snippets embedded in markdown files and docstrings as automated tests, detecting fenced code blocks and inline examples.
Yes. The plugin solves a real problem—keeping documentation examples in sync with code—with low friction and active maintenance. MIT licensing removes legal concerns. It integrates cleanly into existing pytest workflows via a command-line flag. Suitable for projects that prioritize documentation quality and want automated validation of code examples.
Install
pytest-markdown-docs on PyPI
pip
pip install pytest-markdown-docsuv
uv add pytest-markdown-docspoetry
poetry add pytest-markdown-docsInstalling pytest-markdown-docs
Before you install
Low install friction with only two runtime dependencies (pytest and markdown-it-py). Active maintenance with a recent release on 2026-03-23 and last commit on 2026-06-11.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
Quickstart
pip install pytest-markdown-docs
# In a markdown file or docstring:
# ```python
# import mymodule
# result = mymodule.Foo().bar()
# assert result == "hello"
# ```
pytest --markdown-docs
Requires Python 3.9 or later; pytest must be installed as a runtime dependency.
Verify before relying
- Whether line number reporting in tracebacks is sufficiently accurate for debugging docstring-inlined tests.
- Performance impact when running against large markdown files or many code snippets.
- Compatibility with pytest plugins that also modify test discovery or execution.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — markdown-it-py, pytest |
| Maintenance | actively maintained — 144 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 300,279/month — #7,843 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_markdown_docs-0.9.2-py3-none-any.whl
Tags
More Testing packages
Pluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
virtualenvvirtualenv creates isolated Python environments…
permissive · top 100 on PyPI
coverageCoverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
pytest-asynciopytest-asyncio is a pytest plugin that enables…
permissive · top 1,000 on PyPI
pytest-json-ctrfA pytest plugin that generates test reports in…
permissive · top 1,000 on PyPI
markdown-execExecutes code blocks embedded in Markdown files…
permissive · top 5,000 on PyPI
pytest-codeblocksA pytest plugin that automatically discovers…
permissive · top 15,000 on PyPI
pytest-describeA pytest plugin that organizes tests into…
permissive · top 5,000 on PyPI
lib-detect-testenvDetects whether code is running in a test…
permissive · top 5,000 on PyPI
doccmdRuns linters, formatters, and other…
permissive · top 15,000 on PyPI
sybilSybil parses and executes code examples…
permissive · top 15,000 on PyPI
pytest-mdGenerates Markdown test reports from pytest…
permissive · top 15,000 on PyPI
pytest-examplesA pytest plugin that finds, lints, runs, and…
permissive · top 15,000 on PyPI
pytest-doctestplusA pytest plugin that runs doctests in Python…
permissive · top 15,000 on PyPI
sybil-extrasProvides specialized evaluators and parsers…
permissive · top 15,000 on PyPI