init.py


Overview

This file serves as the core of the pytest configuration and plugin management system. It implements:

This file integrates tightly with pytest’s plugin and hook infrastructure, enabling flexible plugin discovery, registration, and hook dispatching. It also manages configuration parsing from command line, ini files, and environment variables, and coordinates the loading and execution of plugins and conftest files.


Detailed Documentation

Constants and Types


Enum: ExitCode

class ExitCode(enum.IntEnum)

Defines standard exit codes for pytest runs:

ExitCode

Value

Meaning

`OK`

0

All tests passed.

`TESTS_FAILED`

1

Some tests failed.

`INTERRUPTED`

2

pytest was interrupted.

`INTERNAL_ERROR`

3

Internal error occurred.

`USAGE_ERROR`

4

Usage error or misconfiguration.

`NO_TESTS_COLLECTED`

5

No tests were collected.

*Introduced in pytest 5.0.*


Exception: ConftestImportFailure

class ConftestImportFailure(Exception)

Raised when importing a `conftest.py` file fails.

try:
    # import conftest logic
except Exception as e:
    raise ConftestImportFailure(conftest_path, cause=e)

Functions

main(args=None, plugins=None) -> int | ExitCode

Perform an in-process pytest test run.

exit_code = main(["-v", "tests/"])
sys.exit(exit_code)

console_main() -> int

Entry point for pytest CLI execution.


filename_arg(path: str, optname: str) -> str

Argparse validator ensuring the given path is a filename (not a directory).


directory_arg(path: str, optname: str) -> str

Argparse validator ensuring the given path is a directory.


get_config(args=None, plugins=None) -> Config

Creates and returns a new `Config` object with default plugins loaded.


get_plugin_manager() -> PytestPluginManager

Returns a new `PytestPluginManager` instance with default plugins loaded.


Class: PytestPluginManager

class PytestPluginManager(PluginManager)

**Description:** An extension of `pluggy.PluginManager` tailored for pytest’s plugin system, with support for:

**Key Attributes:**

**Key Methods:**

**Usage Example:**

pm = PytestPluginManager()
pm.consider_preparse(["-p", "no:cacheprovider"], exclude_only=True)
pm.import_plugin("myplugin")

Class: Config

class Config

**Description:** Central configuration object for pytest. It provides access to:

**Initialization:**

config = Config(pluginmanager)

**Key Attributes:**

**Important Methods:**

**Nested Types:**

**Usage Example:**

config = get_config(["-v", "tests"])
verbosity = config.get_verbosity()
option_val = config.getoption("maxfail", default=5)

Utility Functions


Important Implementation Details


Interactions with Other Parts of the System


Visual Diagram: Class Structure of Key Entities

classDiagram
    class PytestPluginManager {
        -_conftest_plugins: set
        -_dirpath2confmods: dict
        -_confcutdir: pathlib.Path | None
        -_noconftest: bool
        -skipped_plugins: list
        -rewrite_hook: RewriteHook
        -_configured: bool
        +register(plugin, name=None): str | None
        +import_plugin(modname, consider_entry_points=False): None
        +consider_preparse(args, exclude_only=False): None
        +consider_pluginarg(arg): None
        +consider_conftest(conftestmodule, registration_name): None
        +_try_load_conftest(anchor, importmode, rootpath, ...): None
        +_loadconftestmodules(path, importmode, rootpath, ...): None
        +_importconftest(conftestpath, importmode, rootpath, ...): ModuleType
        +pytest_configure(config): None
    }

    class Config {
        +option: argparse.Namespace
        +pluginmanager: PytestPluginManager
        +invocation_params: InvocationParams
        +stash: Stash
        +hook: pluggy.HookRelay
        +rootpath: pathlib.Path
        +inipath: pathlib.Path | None
        +parse(args, addopts=True): None
        +getoption(name, default=..., skip=False): Any
        +getini(name): Any
        +add_cleanup(func): None
        +issue_config_time_warning(warning, stacklevel): None
        +get_terminal_writer(): TerminalWriter
    }

    Config "1" *-- "1" PytestPluginManager : contains

Summary

The [__init__.py](/projects/286/67254) file of the `pytest.config` package provides the essential foundation for pytest’s configuration and plugin system. It implements:

Together, these components enable pytest's flexible, extensible architecture that supports rich plugin ecosystems and customizable test runs, while maintaining a clean and consistent user experience.


End of Documentation for init.py