Skip to main content

Test code from RST documents with pytest

Project description

Tests Latest Version https://img.shields.io/pypi/wheel/pytest-rst.svg https://img.shields.io/pypi/pyversions/pytest-rst.svg https://img.shields.io/pypi/l/pytest-rst.svg

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"

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:.

# fixtures: tmp_path
p = tmp_path / "test.txt"
p.write_text("hello")
assert p.read_text() == "hello"

Multiple fixtures (comma-separated):

# fixtures: tmp_path, capsys
print("hello")
p = tmp_path / "f.txt"
p.write_text("ok")
assert p.read_text() == "ok"
captured = capsys.readouterr()
assert captured.out == "hello\n"

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.

Versioning

This software follows Semantic Versioning.

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_rst-0.2.3.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

pytest_rst-0.2.3-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_rst-0.2.3.tar.gz.

File metadata

  • Download URL: pytest_rst-0.2.3.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_rst-0.2.3.tar.gz
Algorithm Hash digest
SHA256 a450372bb203d2d1eda92c809cafdd171f0920ff576dcded4f85b294ca08b068
MD5 0a5419ff73da669f03cfaf7ba6c33efa
BLAKE2b-256 16889c4ff99a0b7b12d8da1c77e77e69e6faf7d3529bd2cba931b94f5adcd492

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_rst-0.2.3.tar.gz:

Publisher: publish.yml on mosquito/pytest-rst

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_rst-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: pytest_rst-0.2.3-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

Hashes for pytest_rst-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d69592f335dfb706e7cc526c824f3e68497e9571f9733fbd2738b536aafe83e3
MD5 dbae9025e4ad32be556eddca03b25dcc
BLAKE2b-256 aab00c1060cf6ad03f12f8eee671183463c1152d0aa2442fb812ec7cb73680a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_rst-0.2.3-py3-none-any.whl:

Publisher: publish.yml on mosquito/pytest-rst

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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