Skip to main content

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), and it/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.

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)

Source distribution for pytest-pyspec 1.1.1
File Size Uploaded
pytest_pyspec-1.1.1.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pyspec 1.1.1
File Interpreter ABI Platform
pytest_pyspec-1.1.1-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page