Skip to main content

A plugin that transforms the pytest output into a result similar to the RSpec. It enables the use of docstrings to display results and also enables the use of the prefixes "describe", "with" and "it".

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.

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.0.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.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_pyspec-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 eb0705903f146ee5677ea247fef61f3431d717d97e756cdf284979af6906bc33
MD5 f34166a3ca0d599fe5fc98f440307414
BLAKE2b-256 19a797a9d4f3cdea22406838a68c0d8ca89ea8fe8f94232843a2fc40501746bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_pyspec-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9bb27c45aac35de3a1940d5c269e711a4c51b856f412123d50fe1a0aec0e7508
MD5 6dfb05a4a79d990822dea8a07cd05bc2
BLAKE2b-256 4347f3962f165e22c7718d270d02f924b1baa8ebe9598ab4667e45758aa374de

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