show_fixtures_per_test.py


Overview

This file contains a suite of **pytest tests** designed to validate the behavior of the `--fixtures-per-test` pytest command-line option. The `--fixtures-per-test` option, when used, displays which fixtures are used by each test function during a pytest run, including information about fixture origins and docstrings.

The tests in this file use the `Pytester` testing utility provided by pytest itself (`_pytest.pytester.Pytester`) to programmatically create test modules and conftest files, run pytest with the `--fixtures-per-test` option, and verify the expected output.

In essence, this file ensures that pytest’s fixtures-per-test reporting feature works correctly under various scenarios, such as:


Detailed Explanation of Functions

This file does not define any classes; it only defines standalone test functions. Each test function accepts a single parameter:

1. test_no_items_should_not_show_output(pytester: Pytester) -> None


2. test_fixtures_in_module(pytester: Pytester) -> None


3. test_fixtures_in_conftest(pytester: Pytester) -> None


4. test_should_show_fixtures_used_by_test(pytester: Pytester) -> None


5. test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None


6. test_doctest_items(pytester: Pytester) -> None


7. test_multiline_docstring_in_module(pytester: Pytester) -> None


8. test_verbose_include_multiline_docstring(pytester: Pytester) -> None


Important Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram

flowchart TD
    A[Start Test Function] --> B[Create test files (test modules, conftest.py)]
    B --> C[Run pytest with '--fixtures-per-test']
    C --> D[Capture stdout and return code]
    D --> E{Assertions}
    E -->|Check fixture usage output| F[Validate fixture names, docstrings, locations]
    E -->|Check no unwanted output| G[Ensure private fixtures hidden unless verbose]
    E -->|Check doctest integration| H[Validate doctest item collection]
    F --> I[Pass or Fail test]
    G --> I
    H --> I

Summary

This file is a comprehensive test suite ensuring that pytest’s `--fixtures-per-test` option accurately reports fixture usage across a variety of scenarios. It verifies correct inclusion/exclusion of fixtures, proper handling of fixture docstrings (including multiline), precedence of fixtures between `conftest.py` and test modules, private fixture visibility, and compatibility with doctest items. The tests simulate real-world use cases and check the CLI output for correctness, thereby helping maintain the reliability and correctness of pytest’s fixture reporting feature.