Skip to main content

Pytest plugin for running docstring tests in Markdown blocks

Project description

docspec_test — Docstring tests as documentation, spec and CI

CI PyPI Python License: MIT GitHub Repo

Turn examples in your docstrings into executable tests — without leaving your code.

Why

  • Keep tests next to the API they validate — examples never drift from reality.
  • Examples become living documentation and a precise specification.
  • Perfect for TDD and quick feedback while coding.

Features

  • Docstring fenced blocks executed as tests: python test ...
  • Directives: name, raises=Exception, skip[="reason"], xfail[="reason"]
  • Optional setup / teardown blocks per object
  • Pytest integration (auto-discovered plugin) and a standalone CLI
  • Runtime helpers to validate directly in an interpreter or on call

Install

pip install docspec-test

Quickstart

def add(a: int, b: int) -> int:
    """
    Adds two numbers.

    ```python test
    assert add(1, 2) == 3
    ```
    """
    return a + b

Run pytest (the plugin is discovered via pytest11):

pytest

Directives

Add options to the test block header:

```python test name="custom-name" raises=ValueError skip="reason" xfail
# test code

- name: custom test name
- raises: assert that an exception is raised (e.g. `raises=KeyError`)
- skip: skip test, with optional reason
- xfail: expected failure, with optional reason

Setup / Teardown
----------------

One optional `setup` and `teardown` block per object:

```markdown
```python setup
state = {"x": 0}
state["x"] += 1
assert state["x"] == 1
state.clear()

Runtime validation (no pytest)
------------------------------

```python
from docspec_test import validate_module, execute_docstring_tests_for_object, validate_on_call

# validate an entire module by path or module object
validate_module("path/to/module.py")

# validate a single function/class
execute_docstring_tests_for_object(add)

# validate on first call (or on every call with mode="always")
@validate_on_call
def inc(x: int) -> int:
    """
    ```python test
    assert inc(1) == 2
    ```
    """
    return x + 1

CLI

Validate all Python files in the current directory:

docspec-test

Validate a specific path (file or directory):

docspec-test path/to/src

Ignore directories:

docspec-test . --ignore .venv --ignore build

Pytest defaults

By default, only docspec-marked tests run (-m docspec). To run everything:

pytest -m 'not docspec'

Contributing

We’d love your help! Great first issues: docs, new directives, better error reporting, CI workflows.

Dev setup:

git clone https://github.com/alexsukhrin/docspec_test
cd docspec_test
python -m venv venv && source venv/bin/activate
pip install -e .[dev]  # or: pip install -e . && pip install black isort pytest build twine

Run checks:

isort . && black . && pytest -m 'not docspec' && pytest

Roadmap

  • Parametrized examples (table-driven)
  • Inline output matching (like doctest) alongside code blocks
  • Jupyter notebooks support
  • VSCode/IDE integration for quick-run

Requirements

  • Python >= 3.8
  • pytest >= 7.0.0

License

MIT

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

docspec_test-0.1.6.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

docspec_test-0.1.6-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file docspec_test-0.1.6.tar.gz.

File metadata

  • Download URL: docspec_test-0.1.6.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docspec_test-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1eec4f23d0bae511d57ab08f5dce43dc0bdaa471ef623cd85a536c3bbaf12664
MD5 a67ad603e4c4f24ca74a2129c196ed12
BLAKE2b-256 c7934913f9728de38a8117d0dfcfd7b1ada7b88013ab5c0eb69a0f052bb4e0db

See more details on using hashes here.

File details

Details for the file docspec_test-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: docspec_test-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for docspec_test-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 86db2bdf9b517376079022d9fb6dce9e69ec5dc40743708ed2877c549616521a
MD5 c75aea88267086988134736e9505519b
BLAKE2b-256 fa237f736db7adfd7658322199424d5461f99ccc641f0ccb31902771d46d9e2b

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