Test code from RST documents with pytest
Project description
A pytest plugin that discovers and runs Python code blocks embedded in reStructuredText files as test cases.
Installation
pip install pytest-rst
Usage
Add a :name: option starting with test_ to any Python code block in your .rst files, and pytest will collect and run it automatically:
.. code-block:: python
:name: test_example
assert 2 + 2 == 4
assert 2 + 2 == 4
Code blocks without a test_ name are ignored:
.. code-block:: python
# this will not be collected by pytest
assert False
# this will not be collected by pytest
assert False
Fixtures
Code blocks can request pytest fixtures. Fixtures are injected into the code block’s namespace. There are two syntaxes: a directive option and a comment.
Directive option (:fixtures:)
Use the :fixtures: directive option on the code block:
.. code-block:: python
:name: test_file_ops
:fixtures: tmp_path
p = tmp_path / "test.txt"
p.write_text("hello")
assert p.read_text() == "hello"
Versioning
This software follows Semantic Versioning.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_rst-0.2.2.tar.gz.
File metadata
- Download URL: pytest_rst-0.2.2.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b880767c3891d1c1372dbd8059eabf553c81c635bf51660048f0ba33b5ee1b0
|
|
| MD5 |
f66b0d982125d57ec40df222f2476bdd
|
|
| BLAKE2b-256 |
9f36f0fa0f185c50738b6e389963379dcdafa97b1c0c53518dc13977c2b8a6b1
|
Provenance
The following attestation bundles were made for pytest_rst-0.2.2.tar.gz:
Publisher:
publish.yml on mosquito/pytest-rst
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_rst-0.2.2.tar.gz -
Subject digest:
2b880767c3891d1c1372dbd8059eabf553c81c635bf51660048f0ba33b5ee1b0 - Sigstore transparency entry: 976138001
- Sigstore integration time:
-
Permalink:
mosquito/pytest-rst@b6a1ec7e071d906ba9fd543d8ee4709b190ca126 -
Branch / Tag:
refs/tags/0.2.2 - Owner: https://github.com/mosquito
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b6a1ec7e071d906ba9fd543d8ee4709b190ca126 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_rst-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pytest_rst-0.2.2-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b244b70d531dfa01a184e8a5c650666f40694be7ca6e296a90efaf1ff12900e
|
|
| MD5 |
7df204ea873e48b7a76a4f998b492c6c
|
|
| BLAKE2b-256 |
5b5f3054eefb5521ae1569ac3f00fdc51b5f451f3312fd38926538fd32ddcfba
|
Provenance
The following attestation bundles were made for pytest_rst-0.2.2-py3-none-any.whl:
Publisher:
publish.yml on mosquito/pytest-rst
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_rst-0.2.2-py3-none-any.whl -
Subject digest:
0b244b70d531dfa01a184e8a5c650666f40694be7ca6e296a90efaf1ff12900e - Sigstore transparency entry: 976138005
- Sigstore integration time:
-
Permalink:
mosquito/pytest-rst@b6a1ec7e071d906ba9fd543d8ee4709b190ca126 -
Branch / Tag:
refs/tags/0.2.2 - Owner: https://github.com/mosquito
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b6a1ec7e071d906ba9fd543d8ee4709b190ca126 -
Trigger Event:
release
-
Statement type:
Comment syntax (# fixtures:)
Use a # fixtures: comment inside the code block. This is recommended when your RST files are uploaded to PyPI, because PyPI’s renderer rejects unknown directive options like :fixtures:.
Multiple fixtures (comma-separated):
The # fixtures: line is stripped before execution and does not affect your code. Both syntaxes can be combined — fixture names are merged.
Any pytest fixture works, including custom ones defined in conftest.py.