output.rst
Overview
The `output.rst` file is a comprehensive documentation guide focused on managing and customizing the output produced by pytest, the popular Python testing framework. It explains how users can control pytest’s output verbosity, traceback formatting, truncation limits, and result reporting formats such as JUnit XML. The file also covers advanced features like recording custom properties and attributes in test reports, sending test reports to online pastebin services, and fine-grained control over output verbosity.
This document is primarily aimed at pytest users and plugin developers who want to tailor the test output to their needs for better debugging, reporting, and integration with Continuous Integration (CI) systems.
Detailed Explanations
Traceback Printing Modifications
Pytest provides several command-line options to customize how Python tracebacks are displayed when tests fail or errors occur.
--showlocalsor-l
Show local variables in tracebacks, providing richer debugging information.--no-showlocals
Hide local variables even if enabled by default.--capture=fd | sys | no | tee-sys or
-s
Control output capturing:fd (default): capture at the file descriptor level.
sys: capture at the sys module level.noor-s: disable capturing, output goes directly to the terminal.tee-sys: capture output but also show it on sys streams.
--tb=auto | long | short | line | native | no
Control traceback formatting style:auto(default): long tracebacks for first/last entry, short for others.long: full detailed tracebacks.short: concise tracebacks.line: one line per failure.native: Python’s standard library formatting.no: no traceback shown.
--full-trace
Show very long tracebacks on errors and also on KeyboardInterrupt (Ctrl+C), useful to debug hanging tests.
**Usage Example:**
pytest --showlocals --tb=long --capture=no
Verbosity Control
Pytest output verbosity can be controlled using the `-q` (quiet) and `-v` (verbose) flags. Higher verbosity levels provide more detailed information about tests being executed and failed assertions.
-qor--quiet
Less verbose output.-v
More verbose, shows individual test names and more details.-vv
Even more verbose, shows detailed assertion diffs and test output.-vvvand above
Not standard in pytest core but may be used by plugins.
**Examples:**
Running tests with default verbosity:
pytest --no-headerIncreasing verbosity:
pytest --no-header -v pytest --no-header -vv
**Effect on Output:** Increasing verbosity changes how much detail is shown about each test, including full diffs for assertion failures, test function names, and untruncated output.
Fine-Grained Verbosity
Pytest allows independent control over specific verbosity aspects via configuration options:
verbosity_assertions
Controls verbosity of assertion failure output.verbosity_test_cases
Controls verbosity of test execution output.
These can be set in pytest's config files (`pytest.ini`, `tox.ini`, or `setup.cfg`) to fine-tune output behavior beyond the global `-v` flag.
Detailed Summary Reports (-r flag)
The `-r` option produces a short summary of test outcomes at the end of the test run, showing failures, skips, errors, and other statuses.
Default:
-r fE(failures and errors)Customizable flags (can combine multiple letters):
f= failedE= errors= skippedx= xfailed (expected failures)X= xpassed (unexpected passes)p= passedP= passed with outputa= all except passed and passed-with-outputA= allN= none (no summary)
**Example:**
pytest -rfs
Shows only failed and skipped tests in the summary.
Truncation Limits
Introduced in pytest 8.4, users can configure how much output is truncated in assertion failures.
truncation_limit_lines(default 8)
Maximum lines to show before truncating.truncation_limit_chars(default 640)
Maximum characters to show before truncating.
Setting both to 0 disables truncation entirely.
**Configuration (`pytest.ini`):**
[pytest]
truncation_limit_lines = 10
truncation_limit_chars = 90
JUnit XML Report Generation
Pytest can produce test result files in JUnit XML format for CI systems like Jenkins.
Basic usage:
pytest --junit-xml=path/to/results.xmlConfigure root test suite name via config:
[pytest] junit_suite_name = my_suiteConfigure time reporting mode (total test time or call duration):
[pytest] junit_duration_report = call
Recording Additional Information in Test Reports
record_property Fixture
Allows adding custom `` elements inside the `` XML element.
**Example:**
def test_function(record_property):
record_property("example_key", 1)
assert True
Generates XML:
<testcase ...>
<properties>
<property name="example_key" value="1" />
</properties>
</testcase>
record_xml_attribute Fixture
Adds or overrides attributes on the `` element directly.
**Example:**
def test_function(record_xml_attribute):
record_xml_attribute("assertions", "REQ-1234")
record_xml_attribute("classname", "custom_classname")
assert True
Generates XML:
<testcase classname="custom_classname" assertions="REQ-1234" ...>
...
</testcase>
*Note:* This is experimental and may break schema validations in some CI tools.
record_testsuite_property Fixture
Session-scoped fixture to add properties at the `` level, affecting all tests.
**Example:**
@pytest.fixture(scope="session", autouse=True)
def log_global_env_facts(record_testsuite_property):
record_testsuite_property("ARCH", "PPC")
record_testsuite_property("STORAGE_TYPE", "CEPH")
Generates XML:
<testsuite ...>
<properties>
<property name="ARCH" value="PPC"/>
<property name="STORAGE_TYPE" value="CEPH"/>
</properties>
...
</testsuite>
Sending Test Reports to Online Pastebin Services
Pytest can post failure logs or full test session logs to pastebin services (currently https://bpaste.net/).
Submit only failures:
pytest --pastebin=failedSubmit entire test session log:
pytest --pastebin=all
If the upload fails, pytest will emit a warning but continue.
Implementation Details & Algorithms
This file is primarily documentation and examples rather than executable code. It does not contain direct implementations of pytest features but describes how pytest’s internal mechanisms behave and how users can configure them via CLI flags or config files.
Notable aspects covered:
Traceback formatting offers multiple styles, including Python-native and customized verbosity.
Output capturing is flexible, capturing at file descriptor or sys module levels, or disabled entirely.
Verbosity is layered, impacting everything from simple progress dots to detailed assertion diffs.
Truncation logic uses line and character count limits to balance readability and completeness.
XML reporting extensions allow adding custom data to standard JUnit XML, facilitating integration with CI tools.
Pastebin integration provides a simple way to share failure logs externally.
Interaction With Other System Components
Pytest CLI and Configuration: The file documents how pytest command-line options and configuration files influence test output.
Test Execution and Reporting: The verbosity and traceback settings directly affect the test runner’s console output and failure reports.
JUnit XML Exporters: The file describes how to customize XML reports generated by pytest for CI integration.
Plugins and Fixtures: It explains how fixtures like
record_property,record_xml_attribute, andrecord_testsuite_propertyextend pytest’s reporting capabilities.External Services: Pastebin integration allows pytest to communicate with external paste services to upload logs.
Usage Examples Summary
Show locals in traceback with long tracebacks:
pytest --showlocals --tb=longRun tests quietly:
pytest -qRun tests with verbose output showing full assertion diffs:
pytest -vvGenerate JUnit XML report:
pytest --junit-xml=results.xmlRecord custom property in a test:
def test_example(record_property): record_property("priority", "high") assert True
Visual Diagram
The file is a **utility-like documentation file** describing multiple independent functions and features related to pytest output management. Thus, a flowchart representing main topics and their relationships is most appropriate.
flowchart TD
A[Managing pytest Output] --> B(Traceback Printing)
A --> C(Verbosity Control)
A --> D(Detailed Summary Reports)
A --> E(Truncation Limits)
A --> F(JUnit XML Reports)
A --> G(Recording Test Properties)
A --> H(Pastebin Integration)
B --> B1[--showlocals / --no-showlocals]
B --> B2[--capture modes]
B --> B3[--tb traceback styles]
B --> B4[--full-trace]
C --> C1[-q / --quiet]
C --> C2[-v / -vv / -vvv]
C --> C3[Fine-grained verbosity config]
D --> D1[-r flag summary chars]
D --> D2[Skip folding]
E --> E1[truncation_limit_lines]
E --> E2[truncation_limit_chars]
F --> F1[--junit-xml option]
F --> F2[junit_suite_name]
F --> F3[junit_duration_report]
G --> G1[record_property fixture]
G --> G2[record_xml_attribute fixture]
G --> G3[record_testsuite_property fixture]
H --> H1[--pastebin=failed]
H --> H2[--pastebin=all]
Summary
The `output.rst` file serves as an essential guide to customizing pytest output for diverse use cases, including debugging, CI reporting, and enhanced test result visibility. It covers configuration options, command-line flags, practical examples, and advanced features for extending test reports with additional metadata. Users can leverage this information to tailor their test runs and integrate pytest seamlessly into complex development workflows.
**End of Documentation**