pytest-codeblocks
Test code blocks in your READMEs
What it is and what it does
pytest-codeblocks is a pytest plugin that treats code blocks in markdown files—particularly READMEs—as executable test cases. It discovers blocks marked with python, sh, bash, or zsh syntax highlighting and runs them through pytest, letting you verify that documentation examples actually work.
The plugin supports marking code blocks with pytest directives (skip, skipif, xfail) via HTML comments, merging fragmented examples across multiple blocks, and validating expected output. This catches a common documentation debt: examples that drift out of sync with the actual codebase. It depends only on pytest and requires Python 3.10 or later.
Use it for:
- Verify that code examples in a project's README actually execute without errors.
- Catch breaking changes when refactoring by running documented usage patterns as tests.
- Validate shell command sequences and their output in installation or setup documentation.
- Ensure multi-step code examples work when split across separate markdown blocks.
- Skip or mark code blocks as expected-to-fail for examples that demonstrate error handling.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A pytest plugin that automatically discovers and runs Python and shell code blocks from README and markdown files as test cases.
Yes. Low install friction, no security vulnerabilities, permissive MIT license, and active maintenance make this a straightforward addition to any project with documentation that includes runnable code. It solves a real problem—keeping examples in sync with code—with minimal overhead.
Install
pytest-codeblocks on PyPI
pip
pip install pytest-codeblocksuv
uv add pytest-codeblockspoetry
poetry add pytest-codeblocksInstalling pytest-codeblocks
Before you install
Low friction install with a single runtime dependency on pytest. Actively maintained with a release within the last 60 days and marked as Production/Stable.
License in practice
MIT license permits unrestricted use, modification, and distribution with minimal restrictions—suitable for both open-source and commercial projects.
Quickstart
pip install pytest-codeblocks
# In your README.md, add code blocks with python or sh syntax highlighting
# Then run:
pytest --codeblocks
Requires Python 3.10 or later.
Verify before relying
- Whether the plugin handles edge cases like nested code blocks or complex markdown structures.
- Performance characteristics when testing large numbers of code blocks across many files.
Package facts
| License | The MIT License (MIT) Copyright (c) 2020-present Nico Schlömer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — pytest |
| Maintenance | actively maintained — 60 days since the last release |
| First released | |
| Downloads | 186,361/month — #9,989 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytest_codeblocks-0.18.0-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
pytest-markdown-docsA pytest plugin that executes Python code…
permissive · top 15,000 on PyPI
pytest_taggingA pytest plugin that lets you tag tests with…
permissive · top 15,000 on PyPI
pytest-mdGenerates Markdown test reports from pytest…
permissive · top 15,000 on PyPI
pytest-doctestplusA pytest plugin that runs doctests in Python…
permissive · top 15,000 on PyPI
pytest-examplesA pytest plugin that finds, lints, runs, and…
permissive · top 15,000 on PyPI
pytest-skip-slowA pytest plugin that skips tests marked with…
permissive · top 15,000 on PyPI
pytest-describeA pytest plugin that organizes tests into…
permissive · top 5,000 on PyPI
pytest-dependencyA pytest plugin that marks tests as dependent…
permissive · top 5,000 on PyPI
markdown-calloutsAdds a cleaner block-level syntax to Markdown…
permissive · top 15,000 on PyPI
pytest-skip-markersA pytest plugin that provides skip markers to…
permissive · top 15,000 on PyPI