skillfed

pytest-codeblocks

Test code blocks in your READMEs

pytest-codeblocks v0.18.0 186.4K downloads/30d#9,989 on PyPI
Permissive 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) Active released

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

uv

uv add pytest-codeblocks

poetry

poetry add pytest-codeblocks

Installing 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

Development Status :: 5 - Production/StableFramework :: PytestIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3

Tags

test code blocks in markdownreadme code example testingpytest markdown pluginvalidate documentation codeshell script testing in docspython snippet verification
documentation-testingmarkdown-validation

More Testing packages

pluggy

Pluggy provides a plugin system that lets you…

permissive · top 100 on PyPI

pytest

pytest is a testing framework that lets you…

permissive · top 100 on PyPI

virtualenv

virtualenv creates isolated Python environments…

permissive · top 100 on PyPI

coverage

Coverage.py measures which lines of Python code…

permissive · top 1,000 on PyPI

pytest-asyncio

pytest-asyncio is a pytest plugin that enables…

permissive · top 1,000 on PyPI

pytest-json-ctrf

A pytest plugin that generates test reports in…

permissive · top 1,000 on PyPI

pytest-markdown-docs

A pytest plugin that executes Python code…

permissive · top 15,000 on PyPI

pytest_tagging

A pytest plugin that lets you tag tests with…

permissive · top 15,000 on PyPI

pytest-md

Generates Markdown test reports from pytest…

permissive · top 15,000 on PyPI

pytest-doctestplus

A pytest plugin that runs doctests in Python…

permissive · top 15,000 on PyPI

pytest-examples

A pytest plugin that finds, lints, runs, and…

permissive · top 15,000 on PyPI

pytest-skip-slow

A pytest plugin that skips tests marked with…

permissive · top 15,000 on PyPI

pytest-describe

A pytest plugin that organizes tests into…

permissive · top 5,000 on PyPI

pytest-dependency

A pytest plugin that marks tests as dependent…

permissive · top 5,000 on PyPI

markdown-callouts

Adds a cleaner block-level syntax to Markdown…

permissive · top 15,000 on PyPI

pytest-skip-markers

A pytest plugin that provides skip markers to…

permissive · top 15,000 on PyPI