Skip to main content

pytest-codeblock

Test your documentation code blocks.

PyPI Version Supported Python versions Build Status Documentation Status llms.txt - documentation for LLMs MIT Coverage

pytest-codeblock is a Pytest plugin that discovers Python code examples in your reStructuredText and Markdown documentation files and runs them as part of your test suite. This ensures your docs stay correct and up-to-date.

Features

  • reStructuredText and Markdown support: Automatically find and test code blocks in reStructuredText (.rst), Markdown (.md) and MDX (.mdx) files. Async code snippets are supported as well.

  • Grouping: Split a single example across multiple code blocks; the plugin concatenates them into one test.

  • Pytest markers support: Add existing or custom pytest markers to the code blocks and hook into the tests life-cycle using conftest.py.

  • Pytest fixtures support: Request existing or custom pytest fixtures for the code blocks.

Prerequisites

  • Python 3.10+

  • pytest is the only required dependency (on Python 3.11+; for Python 3.10 tomli is also required).

Documentation

Installation

Install with pip:

pip install pytest-codeblock

Or install with uv:

uv pip install pytest-codeblock

Configuration

For most use cases, no configuration needed.

By default, all code blocks with a name starting with test_ will be collected and executed as tests. This allows you to have both test and non-test code blocks in your documentation, giving you flexibility in how you structure your examples.

However, if you want to test all code blocks, you can set test_nameless_codeblocks to true in your pyproject.toml:

Filename: pyproject.toml

[tool.pytest-codeblock]
test_nameless_codeblocks = true

If you still want to skip some code blocks, you can use built-in or custom pytest markers.

See the dedicated reStructuredText docs and Markdown docs to learn more about pytestmark directive.

Note, that nameless code blocks have limitations when it comes to grouping.


By default, all code .rst, .md and .mdx files shall be picked automatically.

However, if you need to add another file extension or use or another language identifier for python in codeblock, you could configure that.

See the following example of pyproject.toml configuration:

Filename: pyproject.toml

[tool.pytest-codeblock]
rst_user_codeblocks = ["c_py"]
rst_user_extensions = [".rst.txt"]
md_user_codeblocks = ["c_py"]
md_user_extensions = [".md.txt"]

See customisation docs for more.


In .mdx files, write directives as JSX comments, because MDX does not allow HTML comments:

{/* pytestmark: xfail */}
```python name=test_mdx_example
assert False
```

See customisation docs for more on MDX.

Usage

reStructruredText usage

Any code directive, such as .. code-block:: python, .. code:: python, or literal blocks with a preceding .. codeblock-name: <name>, will be collected and executed automatically by pytest.

code-block directive example

Filename: README.rst

.. code-block:: python
   :name: test_basic_example

   import math

   result = math.pow(3, 2)
   assert result == 9

literalinclude directive example

Filename: README.rst

.. literalinclude:: examples/python/basic_example.py
    :name: test_li_basic_example

See a dedicated reStructuredText docs for more.

Markdown usage

Any fenced code block with a recognized Python language tag (e.g., python, py) will be collected and executed automatically by pytest.

Filename: README.md

```python name=test_basic_example
import math

result = math.pow(3, 2)
assert result == 9
```

See a dedicated Markdown docs for more.

Tests

Run the tests with pytest:

pytest

Troubleshooting

If something doesn’t work, try to add this to your pyproject.toml:

Filename: pyproject.toml

[tool.pytest.ini_options]
testpaths = [
    "**/*.rst",
    "**/*.md",
]

Writing documentation

Keep the following hierarchy.

=====
title
=====

header
======

sub-header
----------

sub-sub-header
~~~~~~~~~~~~~~

sub-sub-sub-header
^^^^^^^^^^^^^^^^^^

sub-sub-sub-sub-header
++++++++++++++++++++++

sub-sub-sub-sub-sub-header
**************************

License

MIT

Support

For security issues contact me at the e-mail given in the Author section.

For overall issues, go to GitHub.

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

Metadata

Release files for pytest-codeblock 0.5.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-codeblock 0.5.11
File Size Uploaded Provenance Actions
pytest_codeblock-0.5.11.tar.gz 34.8 kB Without provenance Details for pytest_codeblock-0.5.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-codeblock 0.5.11
File Interpreter ABI Platform Actions
pytest_codeblock-0.5.11-py3-none-any.whl Python 3 none any Details for pytest_codeblock-0.5.11-py3-none-any.whl

Total release size: 72.1 kB

Release files / pytest_codeblock-0.5.11.tar.gz

Download URL pytest_codeblock-0.5.11.tar.gz
Size 34.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0308dade281e4c344d7094999e6fd5242c3c155a5a0fa358f409e0618acdadd8
BLAKE2b-256 checksum
How to use checksums
6ac9e6fcb7ef50518bacab6969030cba977199dc5d458edce1d40b4a3950a8a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / pytest_codeblock-0.5.11-py3-none-any.whl

Download URL pytest_codeblock-0.5.11-py3-none-any.whl
Size 37.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e51636de479fd7b7daef9fd49917c448e2d04d73a050e8abc27437bf955fdd87
BLAKE2b-256 checksum
How to use checksums
c28e458c4ef39a333504938813a0ab5f4b43dd7350310d0a7a9b0cba8367f3e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Security for pytest-codeblock 0.5.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page