hookspec.py


Overview

The `hookspec.py` file defines **hook specifications** for the pytest testing framework. These specifications declare the formal extension points (hooks) that plugins and conftest files can implement to customize and extend pytest's behavior throughout its lifecycle.

This file leverages the **pluggy** plugin system to declare hooks that cover all phases of pytest operation, including:

Each hook is decorated with [@hookspec](/projects/286/67223) from pluggy and includes detailed docstrings that explain parameters, expected return values, usage notes, and plugin author guidance.

`hookspec.py` is a fundamental part of pytest’s extensibility infrastructure, defining the "API" that plugins implement to influence pytest’s behavior.


Detailed Hook Specifications

Below is an explanation of the main hooks defined in this file, categorized by their functional areas.

1. Initialization Hooks


2. Bootstrapping Hooks


3. Test Collection Hooks


4. Python Test Function Related Hooks


5. Test Execution (Runtest) Hooks


6. Fixture Related Hooks


7. Test Session Related Hooks


8. Assertion Customization Hooks


9. Reporting and Terminal Hooks


10. Skipping and Marker Hooks


11. Error Handling and Debugging Hooks


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Example: Implementing a plugin that adds a custom command-line option:

import pytest

def pytest_addoption(parser):
    parser.addoption("--myopt", action="store_true", help="Enable my option")

def pytest_configure(config):
    if config.getoption("--myopt"):
        print("My custom option enabled")

Example: Skipping collection of certain files:

def pytest_ignore_collect(collection_path, path, config):
    if collection_path.name == "skipme.py":
        return True
    return None

Example: Customizing test collection:

def pytest_collection_modifyitems(session, config, items):
    # reorder items or deselect some
    items[:] = [item for item in items if "slow" not in item.keywords]

Mermaid Diagram: Structure of hookspec.py File (Hook Specifications)

classDiagram
    class HookSpecs {
        <<interface>>
        +pytest_addhooks(pluginmanager)
        +pytest_plugin_registered(plugin, plugin_name, manager)
        +pytest_addoption(parser, pluginmanager)
        +pytest_configure(config)
        +pytest_cmdline_parse(pluginmanager, args)
        +pytest_load_initial_conftests(early_config, parser, args)
        +pytest_cmdline_main(config)
        +pytest_collection(session)
        +pytest_collection_modifyitems(session, config, items)
        +pytest_collection_finish(session)
        +pytest_ignore_collect(collection_path, path, config)
        +pytest_collect_directory(path, parent)
        +pytest_collect_file(file_path, path, parent)
        +pytest_collectstart(collector)
        +pytest_itemcollected(item)
        +pytest_collectreport(report)
        +pytest_deselected(items)
        +pytest_make_collect_report(collector)
        +pytest_pycollect_makemodule(module_path, path, parent)
        +pytest_pycollect_makeitem(collector, name, obj)
        +pytest_pyfunc_call(pyfuncitem)
        +pytest_generate_tests(metafunc)
        +pytest_make_parametrize_id(config, val, argname)
        +pytest_runtestloop(session)
        +pytest_runtest_protocol(item, nextitem)
        +pytest_runtest_logstart(nodeid, location)
        +pytest_runtest_logfinish(nodeid, location)
        +pytest_runtest_setup(item)
        +pytest_runtest_call(item)
        +pytest_runtest_teardown(item, nextitem)
        +pytest_runtest_makereport(item, call)
        +pytest_runtest_logreport(report)
        +pytest_report_to_serializable(config, report)
        +pytest_report_from_serializable(config, data)
        +pytest_fixture_setup(fixturedef, request)
        +pytest_fixture_post_finalizer(fixturedef, request)
        +pytest_sessionstart(session)
        +pytest_sessionfinish(session, exitstatus)
        +pytest_unconfigure(config)
        +pytest_assertrepr_compare(config, op, left, right)
        +pytest_assertion_pass(item, lineno, orig, expl)
        +pytest_report_header(config, start_path, startdir)
        +pytest_report_collectionfinish(config, start_path, startdir, items)
        +pytest_report_teststatus(report, config)
        +pytest_terminal_summary(terminalreporter, exitstatus, config)
        +pytest_warning_recorded(warning_message, when, nodeid, location)
        +pytest_markeval_namespace(config)
        +pytest_internalerror(excrepr, excinfo)
        +pytest_keyboard_interrupt(excinfo)
        +pytest_exception_interact(node, call, report)
        +pytest_enter_pdb(config, pdb)
        +pytest_leave_pdb(config, pdb)
    }

Summary


End of `hooks