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.3.tar.gz (9.7 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.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docspec_test-0.1.3.tar.gz
  • Upload date:
  • Size: 9.7 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.3.tar.gz
Algorithm Hash digest
SHA256 88a854a1884cc42521523e1b37b8d0dd10b896734c40f02c77b4ba2b6478b59d
MD5 56eedb4d8d8bf5c16f16235dd7c0a358
BLAKE2b-256 70ad4757b6d42933661abfa9e9dfd178d522aed595bcdf619cde4db856b019eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: docspec_test-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a80b5dc8b4c1cebb82d0e46517846ab83e0acf7da426823123bb8d4f780ab63f
MD5 9cdadfda61ac45a62c658ec22c4e6f36
BLAKE2b-256 8388fc3685e67764bca2ef01754f92c1c4db177212d23f0d389d9f79eb9808ed

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