test_conftest.py


Overview

This file contains a suite of tests and utility functions targeting the management, discovery, loading, and behavior of `conftest.py` files within the pytest testing framework. `conftest.py` files are special configuration files used by pytest to define fixtures, hooks, and plugins that influence test discovery and execution.

The tests ensure that `conftest.py` files:

The file also includes helper functions for initializing a `PytestPluginManager` with conftests and a test class to verify visibility and scope of fixtures declared in conftests.


Key Entities

Functions

ConftestWithSetinitial(path) -> PytestPluginManager

conftest_setinitial(conftest, args, confcutdir=None) -> None


Classes

TestConftestValueAccessGlobal


TestConftestVisibility


Important Implementation Details and Algorithms


Interaction With Other Parts of the System


Usage Examples from Tests

# Initialize a PytestPluginManager with conftests starting from a path
conftest = ConftestWithSetinitial(Path("/path/to/tests"))

# Load conftests incrementally at different subdirectories
conftest._loadconftestmodules(
    Path("/path/to/tests/subdir"),
    importmode="prepend",
    rootpath=Path("/path/to/tests"),
    consider_namespace_packages=False,
)

# Access a value 'a' from conftest files for a given path
mod, value = conftest._rget_with_confmod("a", Path("/path/to/tests/subdir"))
print(value)

Mermaid Diagram: Flowchart of Main Functions and Relationships in test_conftest.py

flowchart TD
    A[ConftestWithSetinitial(path)] --> B[conftest_setinitial(conftest, args, confcutdir)]
    B --> C[PytestPluginManager._set_initial_conftests]
    D[TestConftestValueAccessGlobal] -->|Uses| A
    D -->|Tests| PytestPluginManager._loadconftestmodules
    D -->|Tests| PytestPluginManager._rget_with_confmod
    E[TestConftestVisibility] -->|Sets up| _setup_tree
    E -->|Runs pytest with various cwd| pytester.inline_run
    F[Individual test_* functions] -->|Use| pytester, PytestPluginManager
    F -->|Test| conftest discovery, import, options, symlink handling

Summary

The `test_conftest.py` file is a comprehensive pytest test suite focused on verifying the correctness and robustness of conftest file discovery, loading, and usage within pytest. It tests multiple scenarios involving directory structures, import modes, filesystem edge cases, and pytest options related to conftests. The tests ensure that conftest files behave as expected as plugins and fixture providers and that pytest's internal plugin manager correctly handles them. The file plays a crucial role in maintaining the integrity of pytest's configuration and plugin system.