Skip to content

Getting started

pip install pytest-given

Requires Python ≥ 3.14 (t-strings — PEP 750 — are part of the step-text API) and pytest ≥ 9.0.

If AI agents work in your repo, also install the bundled agent skills — with pytest-given's own command, or with library-skills alongside the skills of your other dependencies:

pytest-given skills install
# or
uvx library-skills install --claude

Then narrate a test:

import pytest
from pytest_given import attach, given, scenario, then, when


@pytest.fixture
@given('a coffee machine with 10 coffees')
def machine():
    return {'coffees': 10, 'price': 2}


@scenario('Buying a coffee takes one from the machine', tags=['checkout'])
def test_buy_coffee(machine):
    with when('I buy a coffee'):
        machine['coffees'] -= 1
    with then('the machine has 9 coffees left'):
        assert machine['coffees'] == 9
        attach('Machine state', machine)

Run it:

pytest --given-html

This produces given-report/report.html — one file you can open directly in a browser. See HTML report for what it offers, and Markdown report for a text version.

The Coffeeshop example's report shows this same scenario:

✅ Buying a coffee takes one from the machine

To write the report without running your plain tests, see Selecting scenarios.

Next: Scenarios & steps for the full API, or the Coffeeshop report to see what a finished report looks like.