skillfed

pytest-markdown-docs

Run markdown code fences through pytest

pytest-markdown-docs v0.9.2 300.3K downloads/30d#7,843 on PyPI85
Permissive license MIT Active released

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-docs

uv

uv add pytest-markdown-docs

poetry

poetry add pytest-markdown-docs

Installing 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

markdown code snippet testingpytest markdown plugindocstring code examples as testsmarkdown documentation testingexecutable code fence validationpytest doctest alternativemarkdown code block runner
documentation-testingpytest-pluginexample-validation

More Testing packages