Hook Specification Definitions

Purpose

Within pytest’s plugin system, **hook specifications** define the official API that plugins implement to customize and extend pytest’s behavior. This subtopic addresses the need for a formal, well-documented set of hooks that serve as extension points. These hooks allow plugins to intervene at precise moments in the test lifecycle, such as configuration, collection, test execution, and reporting.

By defining hook specifications, pytest establishes a contract between the core framework and plugins, ensuring consistent interaction patterns and enabling clean, maintainable extensibility. This subtopic focuses specifically on the definitions of these hooks, distinct from the plugin manager’s role of loading and managing plugins.

Functionality

Hook specifications are Python function signatures decorated with a marker (`@hookspec`) provided by the [pluggy](/projects/286/67223) library. These function definitions serve as interfaces that plugins can implement to customize pytest’s internal operations.

Key workflows and features unique to this subtopic include:

Relationship with Plugin System and Other Subtopics

The **Hook Specification Definitions** form the foundational contract that the **Plugin System and Hooks** subtopic builds upon. While the plugin manager handles the loading, registration, and dispatch of plugin hooks, the hook specifications define *what* hooks exist and *how* plugins should implement them.

Together, these subtopics enable pytest’s extensibility:

This separation allows pytest to maintain a clean architecture where the core framework and ecosystem plugins interact through a stable, documented interface.

Additionally, hook specifications interact with other subsystems such as:

Diagram

The following flowchart illustrates the core process of hook specification invocation during a pytest test run, highlighting how pytest calls these hooks at various stages to allow plugin intervention.

flowchart TD
    A[Pytest Start] --> B[Plugin Registration]
    B --> C[Call pytest_addhooks]
    B --> D[Call pytest_addoption]
    D --> E[Parse CLI & Config]
    E --> F[Call pytest_configure]
    F --> G[Test Collection Phase]
    G --> H[Call pytest_collect_file / pytest_collection_modifyitems]
    H --> I[Fixture Setup]
    I --> J[Call pytest_fixture_setup]
    J --> K[Test Execution]
    K --> L[Call pytest_runtest_protocol]
    L --> M[Call pytest_runtest_makereport]
    M --> N[Reporting Phase]
    N --> O[Call pytest_terminal_summary]
    O --> P[Pytest Finish]

This flow demonstrates how the hook specifications act as touchpoints where plugins can hook into pytest’s lifecycle, influencing behavior at each stage.


By defining an extensive set of hook specifications, pytest empowers plugin authors with a rich, consistent API to customize almost every aspect of test discovery, execution, and reporting, fostering a vibrant plugin ecosystem and a highly extensible testing framework.