Skip to content

pytest-given

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.

Getting started See a report

The HTML report of the Hotel booking example: a sidebar of status filters and browse options beside a list of scenarios, one expanded to its Given, When and Then steps with glossary terms underlined. The HTML report of the Hotel booking example: a sidebar of status filters and browse options beside a list of scenarios, one expanded to its Given, When and Then steps with glossary terms underlined.

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. The BDD page writes the quick start's scenario that way, for comparison.

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.
  • A Markdown report of the same run, for pull request diffs and for agents.

Written by agents, reviewed by people

Most tests are no longer written by hand. A person describes the behavior they want, in a prompt or a spec, and an AI agent writes the tests along with the code. People review the narrated report first, and the test code behind it where it matters. The diagram shows this loop, and Working with AI agents explains how to set it up.

A loop between people, agents, and artifacts Developers instruct AI agents, which write annotated tests and code. The tests verify the code and generate a report, which domain experts validate. Developers review all the artifacts, feeding back to the agents. Artifacts instruct write give context review verify generate validate give feedback AI Agents Developers Domain Experts Annotated Tests Code Report
Open at full size

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. 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.

Example reports

The Examples page links the test code behind each report.

Development and license

See AGENTS.md for setup, quality gates, and conventions. pytest-given is MIT licensed. The bundled Alpine.js runtime is also MIT, with its notice in THIRD-PARTY-LICENSES.