test_setuponly.py

Overview

`test_setuponly.py` is a test suite designed to validate and verify pytest's fixture setup reporting features, specifically for the pytest command-line options `--setup-only`, `--setup-plan`, and `--setup-show`. These options control how pytest reports the setup and teardown phases of test fixtures without necessarily running the test body (or running it partially).

This file contains multiple pytest test functions that create and run temporary test files with various fixture configurations to ensure that pytest correctly displays fixture setup/teardown actions, supports different fixture scopes, handles parameterized fixtures, autouse fixtures, dynamic fixture requests, output capturing, and even special cases like KeyboardInterrupt exceptions.

The tests leverage the internal pytest `Pytester` utility to programmatically create test modules and conftest files and run pytest subprocesses with the specified setup options, then assert on the output to confirm expected setup/teardown logs.


Detailed Explanation of Key Components

Imports

Fixture: mode

@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module")
def mode(request):
    return request.param

Test Functions

All test functions use the `pytester` fixture to create temporary test files and run pytest subprocesses.


test_show_only_active_fixtures


test_show_different_scopes


test_show_nested_fixtures


test_show_fixtures_with_autouse


test_show_fixtures_with_parameters


test_show_fixtures_with_parameter_ids


test_show_fixtures_with_parameter_ids_function


test_dynamic_fixture_request


test_capturing


test_show_fixtures_and_execute_test


test_setup_show_with_KeyboardInterrupt_in_test


test_show_fixture_action_with_bytes


Important Implementation Details


Interaction with Other Parts of the System


Usage Example

To manually reproduce some tests in this file, you could run pytest with:

pytest --setup-show

or

pytest --setup-only

on your test suite to see detailed fixture setup and teardown information printed to the console.


Mermaid Class Diagram

This file mainly contains test functions; no classes are declared. However, the key entity is the parametrized fixture `mode` and the multiple test functions that use `pytester` and `mode`. Representing this as a flowchart of test execution and fixture injection is more appropriate.

flowchart TD
    A[mode fixture] -->|parametrize| B[test_show_only_active_fixtures]
    A --> C[test_show_different_scopes]
    A --> D[test_show_nested_fixtures]
    A --> E[test_show_fixtures_with_autouse]
    A --> F[test_show_fixtures_with_parameters]
    A --> G[test_show_fixtures_with_parameter_ids]
    A --> H[test_show_fixtures_with_parameter_ids_function]
    B --> I[pytester fixture]
    C --> I
    D --> I
    E --> I
    F --> I
    G --> I
    H --> I
    I --> J[Creates test files and runs pytest subprocess]
    J --> K[Captures output and return code]
    K --> L[Assertions on setup/teardown output]

Summary

`test_setuponly.py` is a comprehensive pytest test module that validates the correctness of pytest's fixture setup reporting CLI options. It tests fixture visibility, scope handling, parameterization, autouse behavior, dynamic fixture requests, output capturing, and error handling. Leveraging pytest's own internal testing utilities, it ensures pytest's user-facing fixture setup output remains accurate and informative across a variety of scenarios.

This file is a critical part of pytest's own test suite, ensuring high quality and reliability of fixture reporting features that aid users in debugging and understanding fixture usage during test runs.