pytest_userver.fixture_markers lets plugins and tests attach typed metadata to fixture functions and discover marked fixtures visible to a pytest request. The implementation belongs to userver and only depends on pytest fixture definitions; it does not require a running service.
For the common userver use cases, prefer the existing decorators:
For custom discovery, such as database initializers or mock setup, use pytest_userver.fixture_markers.mark and pytest_userver.fixture_markers.get_infos directly as described below. See also Functional service tests (testsuite).
Apply the mark to the original function, below the pytest fixture decorator. pytest_userver.fixture_markers.mark returns the same function without executing or wrapping it. Applying it above the fixture decorator is an error.
Each exact metadata type can be attached only once to a function; a duplicate raises ValueError. Distinct metadata types can coexist on the same function. Metadata subclasses are independent types, not matches for their base class.
The following example defines metadata and a decorator for initialization priority. To keep the example self-contained, the fixtures record initialized table names in a list rather than connecting to a database.
Both ordinary and explicitly named fixtures can carry the mark:
pytest_userver.fixture_markers.get_infos returns a dictionary mapping effective pytest fixture names to metadata, including fixture aliases. It includes fixtures that:
Discovery does not execute fixtures. Fixtures need not appear among the test's arguments to be discovered. The consumer explicitly requests their values when needed. Each query returns a fresh dictionary; changing its entries does not change stored metadata. Metadata objects themselves are not deep-copied.
The explicit priority sort in this example is a consumer policy, not behavior of pytest_userver.fixture_markers.get_infos. Without that sort, the dictionary uses the definition order described below.
An override inherits the mark. There is no need to repeat pytest_userver.config.patch, pytest_userver.service.dependency, or a custom marker decorator on an overriding fixture.
pytest_userver.fixture_markers.get_infos examines the definition pytest would call. If it has no metadata of the requested type, discovery searches the override chain from nearest to farthest and uses the first matching mark. Metadata inheritance is independent for each type.
The following test-class override extends the marked module fixture above. The same rule applies when a module fixture overrides a conftest or plugin fixture:
Requesting the original fixture is optional. An override may replace its behavior entirely and still inherit the mark. Normal pytest lookup always executes the winning override, not the original implementation merely because it owns the metadata.
A mark on the override replaces metadata of that type. Other metadata types remain inherited. The following override replaces both metadata and fixture behavior, while keeping its original dictionary position:
The result of pytest_userver.fixture_markers.get_infos is ordered:
Thus metadata can come from the nearest marked override while the ordering position comes from the original marked definition. A fixture name appears only once in the result.
pytest_userver.config.patch and pytest_userver.service.dependency use these inheritance and discovery rules. Both must be applied below the pytest fixture decorator. A config-patch fixture returns a callable accepting the static config and config variables; a service-dependency fixture performs its setup when requested before service startup.
Legacy config-hook lists and pytest_userver.plugins.service.extra_client_deps remain supported. Legacy USERVER_CONFIG_HOOKS run before all marked config patches, including the built-in userver patches. Hooks passed through uservice_oneshot(config_hooks=...) run last.
To run a custom patch after the built-in HTTP client, logging, or other config patches, apply pytest_userver.config.patch below its @pytest.fixture decorator and remove its name from USERVER_CONFIG_HOOKS. Keep the fixture session-scoped and define it in a conftest that covers the affected tests. Within that conftest, define marked fixtures in their required execution order.
An override of a fixture registered only through a legacy hook list does not inherit a marker: legacy registration and marker discovery are distinct. Overrides of built-in marked fixtures inherit their markers and keep their original positions; do not also list those overrides in USERVER_CONFIG_HOOKS, which would apply them twice.
Adapted from the upstream testsuite fixture-marker guide for userver's implementation and Doxygen documentation.