Test code blocks in your READMEs.
This is pytest-codeblocks, a pytest plugin for testing code blocks from README files. It supports Python and shell code.
Install with
pip install pytest-codeblocks
and run pytest with
pytest --codeblocks
================================= test session starts =================================
platform linux -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /path/to/directory
plugins: codeblocks-0.11.0
collected 56 items
example.md ....................... [ 50%]
README.md ....................... [100%]
================================= 56 passed in 0.08s ==================================
pytest-codeblocks will only pick up code blocks with python and sh/bash/zsh
syntax highlighting.
Marking code blocks
It is possible to use pytest.mark for marking code blocks. For example,
to skip a code block use pytest.mark.skip or pytest.mark.skipif:
Lorem ipsum
<!--pytest.mark.skip-->
```python
foo + bar # not working
```
dolor sit amet.
<!--pytest.mark.skipif(sys.version_info <= (3, 7), reason="Need at least Python 3.8")-->
You can skip code blocks on import errors with
<!--pytest-codeblocks:importorskip(sympy)-->
Skip the entire file by putting
<!--pytest-codeblocks:skipfile-->
in the first line.
For expected errors, use pytest.mark.xfail:
The following gives an error:
<!--pytest.mark.xfail-->
```python
1 / 0
```
Merging code blocks
Broken-up code blocks can be merged into one with the pytest-codeblocks:cont prefix
Lorem ipsum
```python
a = 1
```
dolor sit amet
<!--pytest-codeblocks:cont-->
```python
# this would otherwise fail since `a` is not defined
a + 1
```
If you'd like to prepend code that you don't want to show, you can just comment it out; pytest-codeblocks will pick it up anyway:
Lorem ipsum
<!--
```python
a = 1
```
-->
dolor sit amet
<!--pytest-codeblocks:cont-->
```python
# this would otherwise fail since `a` is not defined
a + 1
```
Expected output
You can also define the expected output of a code block:
This
```sh
print(1 + 3)
```
gives
<!--pytest-codeblocks:expected-output-->
```
4
```
Use expected-output-ignore-whitespace if you'd like whitespace differences to
be ignored.
(Conditionally) Skipping the output verfication works by prepending the first
block with skip/skipif (see above).
Metadata
Release files for pytest-codeblocks 0.18.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_codeblocks-0.18.0-py3-none-any.whl | Python 3 | none | any | Details |
Release files / pytest_codeblocks-0.18.0-py3-none-any.whl
| Download URL | pytest_codeblocks-0.18.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3fe944dc505107421204c83e9232e0155eea1279b9425a10bee327079b272efb
|
|
BLAKE2b-256 checksum How to use checksums |
3acb5f40df7db75c0ac2555009287bb97b322f8d79f9f2bfdfb0bdb560834c28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 15, 2026.
Transparency log