Pytest plugin for running docstring tests in Markdown blocks
Project description
docspec_test — Docstring tests as documentation, spec and CI
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/teardownblocks 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file docspec_test-0.1.5.tar.gz.
File metadata
- Download URL: docspec_test-0.1.5.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ae9fa0a46b9898a594d4aa9748ad067ae7aa5cab70196329f9707aa73354fcc
|
|
| MD5 |
7001d8dcccf77a837427c1d5505705d5
|
|
| BLAKE2b-256 |
c35a684f6b4ecf859958010795b32e26aee9ce1268c4263a656ea3fd4ec1a090
|
File details
Details for the file docspec_test-0.1.5-py3-none-any.whl.
File metadata
- Download URL: docspec_test-0.1.5-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b60c1156fcbd368b78bdfb226edb72fbb7d385163e72177a909cf3f8444cb4d
|
|
| MD5 |
1c246efff8169d02171503cf376aee44
|
|
| BLAKE2b-256 |
546f34b0983f622048e6dea0f1d6a199cfa5582425cb14599a673dc02dfef982
|