pytest 8.0 and Python 3.12: Custom Marker Application Differences
0 reputation · 11 Jun 2022, 09:02 UTC
0 reputation · 11 Jun 2022, 09:02 UTC
The goal is to ascertain whether the refactoring of internal marker handling between pytest 7.6 and 8.0 leads to observable differences in how custom markers are attached to test items when the test suite is executed on different supported Python interpreters.
Constraints include the known change in marker internals, the lack of explicit documentation about version‑specific marker behavior, and the need to compare outcomes without altering test code or plugin configurations.
Specific questions:
34775 reputation · 11 Jun 2022, 17:56 UTC
There are no fundamental changes in how pytest 8.0 applies custom markers to test items compared to pytest 7.6 when running under Python 3.12. The core mechanism of attaching markers via decorators remains consistent across these versions and interpreters. However, the visibility and reporting of these markers have evolved, which can create the appearance of divergence in CI/CD pipelines or CLI output.
If you observe differences in marker behavior between pytest 7.6 and 8.0, they are likely caused by the following changes in configuration discovery and validation rather than the application logic itself:
pyproject.toml. Markers defined in [tool.pytest.markers] are now more prominently surfaced via the --markers flag. If markers were previously registered only via conftest.py without corresponding entries in the configuration file, pytest 8.0 may trigger "unknown marker" warnings that were less aggressive in 7.6.To ensure compatibility and consistency across pytest 7.6 and 8.0 on Python 3.12, follow these steps:
pyproject.toml to ensure they are recognized by the pytest 8.0 discovery engine:
[tool.pytest.markers]
my_custom_marker = "description of marker"pytest --markerspytest_collection_modifyitems are not relying on internal pytest 7.x APIs that may have been deprecated in 8.0.Diagnostic Requirement: To provide a more specific recommendation, please clarify if the "difference" is a runtime failure (e.g., tests not being filtered) or a configuration warning (e.g., PytestUnknownMarkWarning).
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 11 Jun 2022, 14:09 UTC
The core distinction between pytest 7.6 and 8.0 isn't in decorator behavior, but in when plugins can add markers after collection starts. In pytest 8.0, markers added by plugins during test execution (post-collection) appear after import-time decorations, whereas 7.6 processed them earlier in the lifecycle.
This timing shift affects class-level marker inheritance when a plugin dynamically adds markers to test classes after their methods have been instantiated. Static markers and those registered in pytest_configure behave identically in both versions. The divergence only surfaces in plugins that modify markers during test runtime rather than during configuration initialization.
To confirm compatibility, test with a minimal suite: define a class with @pytest.mark.foo, then create a conftest.py that registers a marker in pytest_configure. Run pytest --collect-only with both versions under Python 3.12 and compare item.own_markers. Dynamic markers added after collection will show different ordering in 8.0.