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.2.tar.gz (6.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_rst-0.2.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

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

Hashes for pytest_rst-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2b880767c3891d1c1372dbd8059eabf553c81c635bf51660048f0ba33b5ee1b0
MD5 f66b0d982125d57ec40df222f2476bdd
BLAKE2b-256 9f36f0fa0f185c50738b6e389963379dcdafa97b1c0c53518dc13977c2b8a6b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_rst-0.2.2.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.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

Hashes for pytest_rst-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0b244b70d531dfa01a184e8a5c650666f40694be7ca6e296a90efaf1ff12900e
MD5 7df204ea873e48b7a76a4f998b492c6c
BLAKE2b-256 5b5f3054eefb5521ae1569ac3f00fdc51b5f451f3312fd38926538fd32ddcfba

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_rst-0.2.2-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