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.4.tar.gz (10.4 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.4-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docspec_test-0.1.4.tar.gz
  • Upload date:
  • Size: 10.4 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.4.tar.gz
Algorithm Hash digest
SHA256 ba105b4e43d09927e3564c8d165c83e62531f37eebe1a76b4d3e6fbf60943cde
MD5 e4f3bfebeb34c0cad5ba015cc6321b28
BLAKE2b-256 c489fdd1a86fe4538efdafadf698d5e9e436e3958fc74d09ebce2542b766665a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: docspec_test-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0dd9c55d31741f83e15423be2e6b2b5cf85d9996283bd63461bf8aebd9ff3a9a
MD5 d56c2f787ab356ba9855dfe150ca8e94
BLAKE2b-256 1048504731b1201240f95d14f04d1488eef927e729275c63b76ee8da6dfc3c62

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