reference.rst
Overview
The `reference.rst` file serves as the comprehensive API reference documentation for the pytest testing framework. It provides a detailed and structured catalog of pytest's internal constants, functions, classes, fixtures, hooks, configuration options, warnings, exceptions, and environment variables. The file is designed as a centralized resource for developers and users to understand the available pytest features, their usage, parameters, and integration points.
This documentation is part of pytest's official docs and is intended to assist plugin authors, advanced users, and contributors by exposing the full API surface, including public interfaces and extension hooks.
Key functionalities covered include:
Constants and versioning information
Core pytest functions for test control (
fail,skip,raises, etc.)Built-in markers for test metadata and control (
skip,skipif,xfail,parametrize, etc.)Fixtures, including built-in and user-defined fixtures for test setup and resource management
Plugin hooks and their implementation details
Collection and test execution lifecycle objects and classes
Configuration options and environment variables influencing pytest behavior
Warning and exception classes for error handling and reporting
Detailed Documentation of Major Sections
Constants
pytest.__version__
Current pytest version as a string.
Example:import pytest print(pytest.__version__) # e.g. '7.0.0'pytest.HIDDEN_PARAM
Added in version 8.4. A special marker that can be passed to theidsparameter ofMetafunc.parametrizeor theidofpytest.paramto hide a parameter set from the test name. It can only be used once to maintain unique test names.pytest.version_tuple
Added in version 7.0. The pytest version as a tuple of integers and strings (for prerelease versions).
Example:import pytest print(pytest.version_tuple) # e.g. (7, 0, 0) or (7, 0, '0rc1')
Functions
Core pytest functions include:
pytest.approx
Used for approximate floating point comparisons in tests.
Usage example (not detailed in this file but referenced):assert 0.1 + 0.2 == pytest.approx(0.3)pytest.fail(reason, pytrace=True)
Immediately fail a test with a given reason.reason(str): Explanation of the failure.pytrace(bool, optional): Whether to show Python traceback. Defaults toTrue.
Raisespytest.fail.Exception.
pytest.skip(reason, allow_module_level=False)
Skip the current test with a reason.reason(str): Explanation for skipping.allow_module_level(bool, optional): Allows skipping at the module level ifTrue.
Raisespytest.skip.Exception.
pytest.importorskip
Import a module or skip the test if the import fails.pytest.xfail
Mark the test as expected to fail and skip reporting it as a failure.
Raisespytest.xfail.Exception.pytest.exit(reason, returncode=None)
Exit pytest immediately with a reason and optional return code.
Raisespytest.exit.Exception.pytest.main
Entry point to run pytest programmatically.pytest.param(*values, id=None, marks=None)
Used in parameterization to define sets of parameters with optional ids and marks.pytest.raises(expected_exception, *, match=None)
Context manager/assertion helper for verifying expected exceptions in tests.pytest.deprecated_call(match=None)
Context manager to assert that a deprecated call is made.pytest.register_assert_rewrite
Registers a module for assertion rewriting.pytest.warns(expected_warning, match=None)
Context manager to assert warnings are issued.pytest.freeze_includes
Used in freezing pytest for distribution.
Marks
Marks are decorators to add metadata or modify test behavior.
pytest.mark.filterwarnings(filter)
Add warning filters to marked test items, using Python's warning filter syntax.pytest.mark.parametrize
Mark a test function to run with multiple sets of parameters.pytest.mark.skip(reason=None)
Unconditionally skip a test.pytest.mark.skipif(condition, *, reason=None)
Skip a test if a condition is true.pytest.mark.usefixtures(*names)
Indicate that a test uses certain fixtures, without explicitly declaring them as parameters.pytest.mark.xfail(condition=False, *, reason=None, raises=None, run=True, strict=xfail_strict)
Mark a test as expected to fail under certain conditions.Custom Marks
Created dynamically viapytest.mark.<custom_mark_name>(...)decorators. These marks attach metadata accessible via node iteration methods.
Fixtures
Fixtures provide test setup/teardown and resource management.
Declared using
@pytest.fixturedecorator.Can be requested by test functions or other fixtures by declaring parameter names.
Built-in fixtures include
capsys,capfd,caplog,monkeypatch,tmp_path,tmpdir, and more.Fixtures like
pytestconfigprovide access to pytest's configuration.Specialized fixtures such as
pytester(added in 6.2) provide facilities to test pytest itself.Fixtures can depend on other fixtures, enabling complex setup chains.
Example usage of fixture in test:
def test_output(capsys):
print("hello")
out, err = capsys.readouterr()
assert out == "hello\n"
Hooks
Hooks enable customization and extension of pytest's behavior.
Marked with
@pytest.hookimpldecorator.Hook specifications are declared with
@pytest.hookspec.Hooks cover many phases: initialization, collection, test running, reporting, debugging, and interaction.
Examples include:
pytest_collection_modifyitems,pytest_runtest_setup,pytest_runtest_call,pytest_runtest_teardown,pytest_runtest_makereport, etc.Hooks receive objects like
pytest.Itemand allow manipulation or inspection of the test lifecycle.
Collection Tree Objects
These classes represent the test collection hierarchy:
Node: Base class for collection nodes.
Collector: Base class for collectors of test items.
Item: Base class for test items (individual tests).
File, Package, Module, Class, Function: Specific node types representing different levels of test organization.
FSCollector: Filesystem-based collectors.
Session: The top-level collection/session object.
Other Objects
Key classes accessible from fixtures or hooks:
CallInfo: Information about function calls during testing.CollectReport: Reports from collection phase.Config: Configuration object.ExceptionInfo: Wrapper for exceptions.FixtureDef: Fixture definition.Mark,MarkDecorator,MarkGenerator: Marker classes.Metafunc: Represents a function parameterizing interface.PytestPluginManager: Plugin manager.RaisesExc,RaisesGroup: Exception assertion helpers.TerminalReporter,TestReport: Reporting classes.Stash,StashKey: Internal storage helpers.
Global Variables
Special global variables recognized by pytest when defined in test modules or `conftest.py` files:
collect_ignore: List of paths to exclude from collection.collect_ignore_glob: List of glob patterns to exclude.pytest_plugins: Plugins to load.pytestmark: Marks applied globally to all tests in a module.
Environment Variables
List of environment variables that influence pytest behavior, for example:
CI,BUILD_NUMBER: Indicate running in CI.PYTEST_ADDOPTS: Additional command-line options.PYTEST_DEBUG: Enable debug tracing.PYTEST_DISABLE_PLUGIN_AUTOLOAD: Disable automatic plugin loading.PYTEST_PLUGINS: Comma-separated list of plugins to load.PYTEST_CURRENT_TEST: Set internally to indicate the current running test.
Configuration Options
Built-in config options that can be set in `pytest.ini`, `tox.ini`, `pyproject.toml`, or `setup.cfg`:
addopts: Additional CLI options.cache_dir: Cache directory path.collect_imported_tests: Whether to collect tests imported from other modules.console_output_style: Style of console output (classic, progress, count, times).disable_test_id_escaping_and_forfeit_all_rights_to_community_support: Toggle unicode escaping in test IDs.doctest_encoding,doctest_optionflags: Doctest-related settings.empty_parameter_set_mark: Behavior for empty parameter sets.faulthandler_timeout: Timeout for dumping tracebacks.filterwarnings: Warning filters.junit_family,junit_logging,junit_suite_name: JUnit XML reporting configuration.log_cli,log_cli_level,log_file, etc.: Logging configuration.markers: List of allowed markers.minversion: Minimal pytest version required.norecursedirs: Directory patterns to ignore during collection.python_classes,python_files,python_functions: Test discovery naming conventions.pytest_plugins: Plugins to load.testpaths: Directories to search for tests.tmp_path_retention_policy,tmp_path_retention_count: Temporary path retention behavior.verbosity_assertions,verbosity_test_cases: Verbosity control for assertions and test cases.usefixtures: List of fixtures applied to all tests.xfail_strict: Whether unexpected passes for xfail tests fail the suite.
Command-line Flags
Extensive list of pytest CLI options for test selection, reporting, debugging, logging, collection control, and more.
Examples:
-k EXPRESSION: Run tests matching expression.-m MARKEXPR: Run tests with markers matching expression.--maxfail=num: Exit afternumfailures.--capture=method: Set output capture method.--junitxml=path: Output test results in JUnit XML format.--log-cli-level=LEVEL: Set live logging level.--disable-plugin-autoload: Disable automatic plugin loading.--pdb: Start debugger on failures.
Implementation Details & Algorithms
The documentation uses reStructuredText (RST) with Sphinx directives such as
.. autoclass::,.. autofunction::, and.. confval::to automate extraction of API signatures and docstrings.The API reference is structured hierarchically to reflect pytest's modular architecture.
Hooks are extensively documented to show plugin extension points.
Configuration options and environment variables are clearly categorized to facilitate customization.
The file serves as a single point for auto-generating the official API reference web pages.
Interaction with Other System Components
This file documents the pytest core API and thus interacts conceptually with:
The pytest core engine (test discovery, collection, execution, and reporting).
Plugins and hooks that extend pytest's behavior.
User test code which uses pytest features (fixtures, markers, assertions).
Configuration files (
pytest.ini,tox.ini,pyproject.toml) that influence pytest via options documented herein.External tools and CI systems consuming pytest outputs (e.g., JUnit XML reports).
Although this file is not executable code, it forms the backbone for understanding how pytest components interoperate and how users and plugin authors should interact with pytest's API.
Visual Diagram: pytest API Structure Class Diagram
The following Mermaid class diagram summarizes the main classes and their relationships as documented in `reference.rst`. It focuses on the core pytest object model relevant to test collection and execution.
classDiagram
class Node {
+id
+name
+parent
+iter_markers()
}
class Collector {
+collect()
+add_items()
}
class Item {
+runtest()
+setup()
+teardown()
}
class File
class Package
class Module
class Class
class Function
class Session
Node <|-- Collector
Node <|-- Item
Collector <|-- File
Collector <|-- Package
Collector <|-- Module
Collector <|-- Class
Item <|-- Function
Session --> Collector : collects
%% Additional classes for fixtures and markers
class FixtureDef {
+scope
+func
}
class Mark {
+args
+kwargs
}
class Metafunc {
+parametrize()
}
Item o-- FixtureDef : uses
Node o-- Mark : attached to
Metafunc ..> Item : parameterizes
Usage Examples
import pytest
@pytest.mark.parametrize("input,expected", [
(1, 2),
(3, 4),
pytest.param(5, 6, id="special_case", marks=pytest.mark.skip)
])
def test_increment(input, expected):
assert input + 1 == expected
def test_fail_example():
pytest.fail("This test fails deliberately.")
def test_skip_example():
pytest.skip("Skipping this test for demonstration.")
Summary
The `reference.rst` file is a vital document that fully enumerates pytest's API, enabling advanced usage, plugin development, and customization. It covers everything from core functions and fixtures to hooks, markers, configuration, and environment variables, giving a complete picture of pytest's extensibility and functionality.
This documentation facilitates deep understanding and correct usage of pytest's rich feature set, supporting both new users and experienced developers in leveraging pytest effectively.