pytest-examples
Pytest plugin for testing examples in docstrings and markdown files.
What it is and what it does
pytest-examples is a pytest plugin that discovers and validates Python code examples embedded in docstrings and markdown files. It integrates black and ruff to lint examples for style compliance, runs them to verify they execute without error, and can verify that print statements in the code match their expected output (marked with `#>` comments). The plugin can also update examples in place to auto-format code and insert or refresh print statement annotations.
Typical use involves writing a pytest test that parametrizes over examples found in a directory or file, then calling methods like `lint()`, `run()`, `run_print_check()`, or `run_print_update()` on each example. This ensures documentation code stays correct and executable as the underlying library evolves.
Use it for:
- Verify that code examples in API documentation actually run without errors
- Enforce consistent code style in docstrings and README examples using ruff and black
- Validate that printed output in documented examples matches what the code actually produces
- Automatically update and reformat all code examples across a project in one pass
- Catch broken examples early in CI when documentation is changed alongside code
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that finds, lints, runs, and validates Python code examples embedded in docstrings and markdown files, using ruff and black for formatting and checking print statement correctness.
Yes. The plugin fills a real gap—keeping documentation examples in sync with code—and is actively maintained with no known vulnerabilities. Low install friction and permissive MIT license make it a straightforward addition to any pytest-based test suite. Install if you maintain libraries with documented examples and want them to stay correct.
Install
pytest-examples on PyPI
pip
pip install pytest-examplesuv
uv add pytest-examplespoetry
poetry add pytest-examplesInstalling pytest-examples
Before you install
Low friction install with three runtime dependencies (black, pytest, ruff). Active maintenance with recent commits; last release 465 days ago but repository remains actively developed.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions—suitable for most commercial and open-source projects.
Quickstart
pip install pytest-examples
import pytest
from pytest_examples import find_examples, CodeExample, EvalExample
@pytest.mark.parametrize('example', find_examples('docs'), ids=str)
def test_examples(example: CodeExample, eval_example: EvalExample):
eval_example.lint(example)
eval_example.run(example)
Verify before relying
- Whether the plugin works with pytest versions beyond those implicitly tested in CI
- Performance characteristics when scanning large codebases with many examples
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — black, pytest, ruff |
| Maintenance | actively maintained — 465 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 594,886/month — #5,843 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_examples-0.0.18-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
flake8-pytest-styleA flake8 plugin that detects and reports style…
unclear · top 15,000 on PyPI
pytest-doctestplusA pytest plugin that runs doctests in Python…
permissive · top 15,000 on PyPI
pytest-codeblocksA pytest plugin that automatically discovers…
permissive · top 15,000 on PyPI
pytest-mdGenerates Markdown test reports from pytest…
permissive · top 15,000 on PyPI
pytest-markdown-docsA pytest plugin that executes Python code…
permissive · top 15,000 on PyPI
doccmdRuns linters, formatters, and other…
permissive · top 15,000 on PyPI
pytest-ruffA pytest plugin that integrates ruff code…
unclear · top 15,000 on PyPI
docstring-to-markdownConverts Python docstrings from…
copyleft · top 5,000 on PyPI
parametrizeProvides a `@parametrize` decorator that works…
permissive · top 15,000 on PyPI
flake8-quotesA flake8 plugin that enforces consistent quote…
permissive · top 5,000 on PyPI