pytest options¶
A plain pytest run writes no report. Each --given-* output flag turns on one report format, and you can combine them. For example, pass both --given-json and --given-html to get both files from one run.
The checks, however, always run. Every run builds the report, even if it doesn't write it. So a step that can't be reported correctly, like one that breaks the parametrize rules, fails every run, not just the first run that writes a report.
Flags¶
| Flag | Default | Description |
|---|---|---|
--given-json[=PATH] |
off | Write the JSON report data. Without a path: given-report/report-data.json. |
--given-html[=PATH] |
off | Write the HTML report. Without a path: given-report/report.html. |
--given-md[=PATH] |
off | Write the Markdown report. Without a path, it prints to stdout, between <!-- pytest-given:md:start --> and <!-- pytest-given:md:end --> markers. |
--given-md-lines / --no-given-md-lines |
true |
Show the line in the file.py:line::test_name anchor under each Markdown heading. Off, the anchor is the node ID, so a Markdown diff doesn't show every anchor below an inserted test. Ini setting: given_md_lines; both flags override it. |
--given-md-source |
off | End each Markdown scenario with its test function, decorators included, each line under its number in the file, for reviewing step text against step body. Only the test function is shown, not the step fixtures or helpers it uses. No ini setting. |
--given-title=TEXT |
rootdir name | The report's name: the Markdown heading, and the HTML report's browser tab title and top bar. Ini setting: given_title. |
--given-report-url=URL |
unset | The public URL the HTML report will be served from, such as its page on your docs site. The report then shows copy buttons that put a Markdown citation linking there on the clipboard, and pytest-given citations checks links to it. Ini setting: given_report_url. |
--given-source-link=PRESET |
none |
An editor preset (vscode, cursor, zed, pycharm, github) or a URL template. HTML only. Adds a clickable file:line link to each scenario card, story panel, and expanded glossary term. Ini setting: given_source_link. See Source links. |
--given-all-frames |
off | Keep pytest's and pytest-given's internal frames (pluggy, _pytest, pytest-given) in failure tracebacks. See Traceback frames. |
--given-lint / --no-given-lint |
false |
Turn the narration lint on or off. A lint finding with severity error fails the run. Ini setting: given_lint; both flags override it. See Narration lint. |
--given-theme=light\|dark\|auto |
auto |
The theme the HTML report opens in. auto follows the viewer's system setting. The report's own Light / Dark / System switch overrides it in each browser. Ini setting: given_theme. |
Flag order¶
If an output flag has no =PATH, pytest reads the next word on the command line as its path, not as a test to run. To avoid this, either:
- put
--given-json,--given-htmlor--given-mdlast on the command line, or - use the
=PATHform:--given-html=out.html, not--given-html out.html.
If that path can't be a report file, like a .py test file, pytest-given stops before running the tests, so it never overwrites your test.
Selecting scenarios¶
pytest-given marks every @scenario test, each case of a parametrized one included, with the pytest_given marker. You don't apply it yourself: a hand-written @pytest.mark.pytest_given on a plain test makes it selectable, but records nothing. The marker is registered, so --strict-markers accepts it. Select with pytest's own -m:
- A report without running the plain tests. The report holds only scenarios, so
pytest -m pytest_given --given-htmlwrites the same report as a full run, faster. - Code coverage of the scenarios.
pytest -m pytest_given --cov=mypkg(with pytest-cov), orcoverage run -m pytest -m pytest_given. It counts the lines the scenarios run, not the lines a step describes, so it is an upper bound on what the report documents. Compare it with a full run's coverage: lines the suite covers but the scenarios don't are behavior only plain tests reach, and the report doesn't show. - The plain tests alone, with
pytest -m "not pytest_given".
A -m on the command line replaces one in your addopts, so combine them yourself: -m "pytest_given and not slow". In a suite with no scenarios, -m pytest_given deselects every test and pytest exits with code 5.
Mind -k: it matches any part of a test's keywords, and a scenario's include pytest_given and _scenario, so -k given or -k scenario selects every scenario too.
pytest-xdist¶
pytest-given writes reports and runs the narration lint under pytest-xdist with --dist loadfile. Grouping a parametrized scenario needs all its cases on one worker, and loadfile sends each file to one worker. When a -n run asks for a report (--given-json, --given-html, --given-md) or the narration lint, pytest-given turns a bare -n into --dist loadfile. Any other --dist or -d, including one in addopts, stops the run with a usage error. The workers send their scenarios to the main process, which writes the report and runs the lint as a run without -n does. If a worker crashes, the run fails and writes no report. A run interrupted before every worker reported keeps the previous report. Without a report or the lint, --dist is left alone.
Ini settings¶
Put ini settings in [tool.pytest] in pyproject.toml (pytest 9's native TOML table). pytest still reads the older [tool.pytest.ini_options], but you can't use both: pytest raises UsageError if both are present.