Skip to main content

Pytest plugin to collect and test code blocks in reStructuredText and Markdown files.

Project description

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) and Markdown (.md) 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 and .md 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.

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>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_codeblock-0.5.9.tar.gz (32.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_codeblock-0.5.9-py3-none-any.whl (34.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_codeblock-0.5.9.tar.gz.

File metadata

  • Download URL: pytest_codeblock-0.5.9.tar.gz
  • Upload date:
  • Size: 32.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for pytest_codeblock-0.5.9.tar.gz
Algorithm Hash digest
SHA256 7dedca2ef01a383f60b67ddcf10e32e64f94b6614699af3e2089cb5ec5f969e2
MD5 40df00fb833db280a6125c3d64ce4773
BLAKE2b-256 3db477de7de4278a49a3c9c3d1df502b2ee36db73a3f4d387813f383aea01560

See more details on using hashes here.

File details

Details for the file pytest_codeblock-0.5.9-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_codeblock-0.5.9-py3-none-any.whl
Algorithm Hash digest
SHA256 378299e1a6cd640dd467c6733b340f1ebbfd589aea0607eeb8255884f66c1c00
MD5 f1d9184d233344be7e47cfecee381772
BLAKE2b-256 121733373cb131702dacf889a5df9a72970be4e38c097ec01af602d17ca8b53b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page