Skip to content

About

A pytest plugin that generates interactive HTML reports from Given/When/Then annotated tests.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

pytest-given

A pytest plugin that turns Given/When/Then annotated tests into interactive HTML reports.

Given your pytest tests,
when you narrate them with given / when / then,
then documentation and behavior fuse into one report.

What people and agents read is what the code does.

Inspired by JGiven (Java).

Documentation: https://nwilbert.github.io/pytest-given/latest/

Live examples:

Quick start

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')
def machine():
    return {'coffees': 10, 'price': 2}


@scenario('Buy coffee', tags=['billing'])
def test_buy_coffee(machine):
    with when('I insert $2'):
        machine['coffees'] -= 1
    with then('I get a coffee'):
        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.

Why pytest-given?

Classical BDD tools (Cucumber, behave, pytest-bdd) center on Gherkin, a natural-language DSL: stakeholders write the tests, and engineers maintain the glue code behind each step.

pytest-given works the other way round: engineers or their agents write normal tests, and pytest-given turns them into readable documentation. Stakeholders and domain experts can follow the HTML report without ever opening the test suite. Engineers get a view of the system's behavior that is easier to scan than test code, browsable by tag, glossary term, or module. The approach is the one JGiven pioneered for Java, brought to pytest.

  • Plain Python: no Gherkin, no .feature files, no parser.
  • Tests stay first-class pytest tests, and the report is a by-product.
  • Self-contained HTML: open it locally or attach it to a CI run, with no server and no external assets.

Written by agents, reviewed by people

More and more tests aren't written by hand. A person describes a scenario, an AI agent writes the test along with the code, and people review the narrated report instead of the test code. The diagram shows this loop, and Working with AI agents explains how to set it up.

A loop between people, agents, and artifacts: developers and domain experts instruct AI agents, which write annotated tests and code. The tests verify the code and generate a report that domain experts validate and developers review, feeding back to the agents.

Glossary and Domain Storytelling

pytest-given goes beyond JGiven by tying tests to the domain itself. A glossary defines the terms your team uses, and the report highlights them wherever a test mentions them. Domain Storytelling adds the big picture: stories of how the domain works, with the report showing which scenarios cover each sentence of a story.

Documents that stay true

A design spec or a README can cite scenarios, tags and stories as links into the report, each led by its current status. pytest-given citations update keeps the citations in line with the latest run, and pytest-given citations check in CI fails when a cited scenario stops passing or disappears, so the document can't quietly claim what the code no longer does.

Features

  • Step context managers: with given(...), when(...), then(...) blocks in plain pytest tests, nesting within a phase.
  • Narrated fixtures: @given on a fixture, or Annotated[..., given(...)] on a parameter, records setup as a step.
  • Parametrized scenarios: one narrated tree plus a parameter table per case, or one scenario per case on request.
  • Glossary: your domain's terms, declared in code or loaded from a GLOSSARY.md, rendered as highlighted term refs in the narration.
  • Domain Storytelling: Domain Stories as sequences of sentences, and per-sentence coverage in the report.
  • Narration lint: structural checks that a step's text is honest about its body: empty steps, a then that checks nothing, a missing phase.
  • Citations: links from Markdown documents to scenarios, tags and stories, led by their status and checked against the latest run.
  • Agent skills: bundled Agent Skills for authoring, navigating and reviewing narrated tests, installable with one command.

The full reference — pytest options, source links, the standalone CLI — is on the documentation site.

License

MIT. The bundled Alpine.js runtime is also MIT; its notice is in THIRD-PARTY-LICENSES.

About

A pytest plugin that generates interactive HTML reports from Given/When/Then annotated tests.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages