pytest-pyspec
The pytest-pyspec plugin transforms pytest output into a beautiful, readable format similar to RSpec. It provides semantic meaning to your tests by organizing them into descriptive hierarchies, using the prefixes Describe/Test, With/Without/When, and test_/it_, while allowing docstrings and decorators to override the descriptions.
Features
- Semantic Output: Transform pytest's default output into readable, hierarchical descriptions
- Multiple Prefixes: Support for
describe/test(objects),with/without/when(contexts), andit/test(tests) - Docstring Support: Override test descriptions using docstrings
- Consolidated Output: Smart grouping that avoids repeating parent headers
- Natural Language: Automatic lowercase formatting of common words (the, is, are, etc.)
- Decorator Support: Override descriptions right next to your classes and tests
Quick Start
Installation
pip install pytest pytest-pyspec
Running
pytest --pyspec
Examples
Car Scenario
A minimal car example with properties and behaviors:
class DescribeCar:
def test_has_engine(self):
assert True
class WithFullTank:
def test_drive_long_distance(self):
assert True
class WithoutFuel:
def test_cannot_start_engine(self):
assert True
class WhenTheEngineIsRunning:
def test_consumes_fuel(self):
assert True
With pytest-pyspec, this produces:
a Car
✓ has engine
with Full Tank
✓ drive long distance
without Fuel
✓ cannot start engine
when the Engine is Running
✓ consumes fuel
Available Prefixes
pytest-pyspec supports three types of prefixes to create semantic test hierarchies:
1. Object Classes (use describe or test)
Define what you're testing:
class DescribeCar: # or class TestCar:
def test_has_four_wheels(self):
assert True
Output:
a Car
✓ has four wheels
2. Context Classes (use with, without, or when)
Define the context or state:
class DescribeCar:
class WithFullTank:
def test_can_drive_long_distances(self):
assert True
class WithoutFuel:
def test_cannot_start_engine(self):
assert True
class WhenTheEngineIsRunning:
def test_consumes_fuel(self):
assert True
Output:
a Car
with Full Tank
✓ can drive long distances
without Fuel
✓ cannot start engine
when the Engine is Running
✓ consumes fuel
3. Test Functions (use it_ or test_)
Define the expected behavior:
class DescribeCar:
def it_has_four_wheels(self):
assert True
def test_has_engine(self):
assert True
Output:
a Car
✓ has four wheels
✓ has engine
Using Docstrings
Override automatic naming with custom descriptions:
class TestCar:
"""sports car"""
def test_top_speed(self):
"""reaches 200 mph"""
assert True
class WhenTheNitroIsActivated:
"""when nitro boost is activated"""
def test_acceleration(self):
"""accelerates rapidly"""
assert True
Output:
a sports car
✓ reaches 200 mph
when nitro boost is activated
✓ accelerates rapidly
Using Decorators
Prefer decorators over docstrings? Import the helpers directly from pytest_pyspec:
import pytest_pyspec as spec
@spec.describe("Car")
class DescribeCar:
@spec.it("reaches 200 mph")
def test_top_speed(self):
assert True
@spec.when("nitro boost is activated")
class WhenTheNitroIsActivated:
@spec.it("accelerates rapidly")
def test_acceleration(self):
assert True
Output:
a Car
✓ reaches 200 mph
when nitro boost is activated
✓ accelerates rapidly
You can also import individual helpers (describe, with_, without, when,
it) directly from pytest_pyspec if you prefer from ... import ... style.
with is still exposed as with_ because the plain name is reserved.
Decorators always win when both a docstring and a decorator are present, so you can keep docstrings for documentation/IDE help while letting decorators drive runtime output.
Configuration
The plugin is automatically enabled when you use the --pyspec flag. No additional configuration is required.
For more information, see the documentation.
Metadata
Release files for pytest-pyspec 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest_pyspec-1.1.1.tar.gz | 10.7 kB | Without provenance | Details for pytest_pyspec-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_pyspec-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for pytest_pyspec-1.1.1-py3-none-any.whl |
Total release size: 22.2 kB
Release files / pytest_pyspec-1.1.1.tar.gz
| Download URL | pytest_pyspec-1.1.1.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ca07d2ed6c0e780f609bb6081b7a6b7a3f9eeb62b079d6d9e965edc7ba59457c
|
|
BLAKE2b-256 checksum How to use checksums |
7b740a8c4af1fcb40ffb2056b0f58f72e628c8150472beab9c83e6327689fe3b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.8
|
Release files / pytest_pyspec-1.1.1-py3-none-any.whl
| Download URL | pytest_pyspec-1.1.1-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
99714c5bc6eb7c0813e93dc2dc3e2b30762e641a1456fab72e29f9cd467b2a5e
|
|
BLAKE2b-256 checksum How to use checksums |
de1006653ed44748832aec0d036947ec8baea712e5fac459acc8351c18342be9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.8
|
Security for pytest-pyspec 1.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.