Skip to main content

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.

Project description

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.

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

pytest_pyspec-1.1.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_pyspec-1.1.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest_pyspec-1.1.1.tar.gz.

File metadata

  • Download URL: pytest_pyspec-1.1.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for pytest_pyspec-1.1.1.tar.gz
Algorithm Hash digest
SHA256 ca07d2ed6c0e780f609bb6081b7a6b7a3f9eeb62b079d6d9e965edc7ba59457c
MD5 1c901503c1bbee04646abcfec7c67455
BLAKE2b-256 7b740a8c4af1fcb40ffb2056b0f58f72e628c8150472beab9c83e6327689fe3b

See more details on using hashes here.

File details

Details for the file pytest_pyspec-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_pyspec-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99714c5bc6eb7c0813e93dc2dc3e2b30762e641a1456fab72e29f9cd467b2a5e
MD5 cd085547cb6bbd6c87182b0725e1845b
BLAKE2b-256 de1006653ed44748832aec0d036947ec8baea712e5fac459acc8351c18342be9

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