deprecated_test.py
Overview
The [deprecated_test.py](/projects/286/67361) file contains a suite of automated tests focused on verifying the behavior of deprecated features, warnings, and APIs within the pytest framework and its related plugins. The primary goal of this file is to ensure that deprecated constructs in pytest, such as old plugin hooks, fixture usage patterns, and path handling, properly emit warnings or errors when used, thus guiding developers towards updated best practices.
This testing module helps maintain backward compatibility while encouraging migration away from deprecated features, supporting a smoother transition between pytest versions.
Detailed Explanations
Imports and Setup
Standard libraries:
pathlib.Path,re,sysPytest internals and utilities:
_pytest.deprecated,_pytest.compat.legacy_path,_pytest.pytester.Pytesterpytestitself, including warning classes likePytestDeprecationWarning
Tests and Their Purpose
test_external_plugins_integrated(pytester: Pytester, plugin) -> None
Purpose: Verifies that deprecated external plugins listed in deprecated.DEPRECATED_EXTERNAL_PLUGINS still integrate but emit a
PytestConfigWarning.Parameters:
pytester: Pytest's test helper fixture for creating temporary test environments.plugin: The name of each deprecated external plugin.
Behavior:
Inserts the test directory in the system path.
Creates an empty Python file named after the plugin.
Parses the config with the plugin enabled, expecting a configuration warning.
Usage Example:
def test_external_plugins_example(pytester: Pytester):
# Example plugin name from the deprecated list
plugin = "deprecated_plugin_name"
pytester.syspathinsert()
pytester.makepyfile(**{plugin: ""})
with pytest.warns(pytest.PytestConfigWarning):
pytester.parseconfig("-p", plugin)
test_hookspec_via_function_attributes_are_deprecated()
Purpose: Ensures that defining hook specifications using function attributes (instead of decorators) triggers a
PytestDeprecationWarning.Implementation Details:
Defines a class with a hook method decorated by setting a
.historicattribute.Adds the hookspecs to a
PytestPluginManagerinstance.Checks that a deprecation warning references the exact code line and file.
Key Points:
Encourages using
@pytest.hookspec(historic=False)decorator instead.
test_hookimpl_via_function_attributes_are_deprecated()
Purpose: Validates that marking hook implementations with function attributes (e.g.,
.tryfirst = True) emits a deprecation warning.Implementation Details:
Defines a plugin class with a hook method, using attribute-based marker.
Registers the plugin on
PytestPluginManager.Asserts the warning is raised and points to the proper source code location.
Encouraged Usage:
Use
@pytest.hookimpl(tryfirst=True)instead of function attribute assignment.
test_yield_fixture_is_deprecated()
Purpose: Confirms that using
@pytest.yield_fixtureraises aDeprecationWarning.Implementation Details:
Defines a simple yield fixture inside a warning context.
Note: Modern pytest favors
@pytest.fixturewithyieldinstead of@yield_fixture.
test_private_is_deprecated()
Purpose: Tests that instantiating a private pytest class or function without the
_ispytestflag triggers a deprecation warning.Details:
Defines a class
PrivateInitwith a special_ispytestparameter checked bydeprecated.check_ispytest.Warning is raised if
_ispytestis omitted or False.
Usage:
Internal pytest code should pass
_ispytest=Trueto suppress warnings.External code should expect warnings when accessing private APIs.
test_hookproxy_warnings_for_pathlib(tmp_path, hooktype, request)
Purpose: Tests that using deprecated
py.path.localarguments in hook calls emits deprecation warnings, especially when mixing withpathlib.Path.Parameters:
tmp_path: pytest temporary path fixture.hooktype: Either"hook"or"ihook"to test two hook proxy types.request: pytest fixture that provides access to test context.
Behavior:
Calls the hook
pytest_ignore_collectwith both deprecatedpathand newcollection_patharguments.Asserts warning is triggered and points to the correct source line.
Checks that mismatched path arguments raise a
ValueError.
test_hookimpl_warnings_for_pathlib()
Purpose: Checks that registering plugins with deprecated
py.path.localarguments on hookimpl methods raisePytestRemovedIn9Warning.Details:
Defines a plugin class with multiple hookimpl methods expecting
patharguments.Registers the plugin to a
PytestPluginManager.Expects one warning per deprecated hookimpl method.
test_node_ctor_fspath_argument_is_deprecated(pytester: Pytester)
Purpose: Verifies that passing
fspath: py.path.localargument to node constructors (likepytest.File) emits a deprecation warning.Details:
Creates a subclass of
pytest.Filewith a stubcollectmethod.Calls
from_parentwith a deprecatedfspathargument.Checks that a deprecation warning referencing the class name and argument is raised.
Tests for Fixture and Mark Warnings
These tests ensure that applying pytest marks (like `@pytest.mark.parametrize` or `@pytest.mark.usefixtures`) to fixtures or vice versa warns users because marks on fixtures currently have no effect, which may confuse users.
test_fixture_disallow_on_marked_functions()test_fixture_disallow_marks_on_fixtures()test_fixture_disallowed_between_marks()
Each test:
Defines a function with a combination of fixture and mark decorators in various orders.
Asserts that
PytestRemovedIn9Warningwarnings are emitted.Checks the number of warnings and that they point to this test file.
Important Implementation Details and Algorithms
The tests heavily utilize
pytest.warnscontext managers to capture and assert specific deprecation warnings.Some tests introspect warnings to check source line numbers and filenames to ensure warnings point to correct user code locations.
The
legacy_pathutility is used to simulate deprecatedpy.path.localinstances.Several tests use parametrization and fixtures (
pytester,tmp_path,request) to modularize and reuse test setups.The file does not define reusable classes or functions but rather a collection of test functions each targeting a specific deprecated feature or API.
Interactions within the System
The tests depend on internal pytest modules such as
_pytest.deprecatedand_pytest.compatto identify deprecated APIs.They interact with
Pytesterto create isolated test environments for plugin and node testing.They verify behavior of
PytestPluginManagerfor plugin registration and hook specification handling.The file helps ensure pytest core and plugin ecosystem maintain consistent deprecation policies.
The tests indirectly affect development and maintenance workflows by validating that deprecation warnings are properly emitted, guiding developers away from outdated practices.
Visual Diagram: Class and Test Function Structure
flowchart TD
A[test_external_plugins_integrated]
B[test_hookspec_via_function_attributes_are_deprecated]
C[test_hookimpl_via_function_attributes_are_deprecated]
D[test_yield_fixture_is_deprecated]
E[test_private_is_deprecated]
F[test_hookproxy_warnings_for_pathlib]
G[test_hookimpl_warnings_for_pathlib]
H[test_node_ctor_fspath_argument_is_deprecated]
I[test_fixture_disallow_on_marked_functions]
J[test_fixture_disallow_marks_on_fixtures]
K[test_fixture_disallowed_between_marks]
subgraph Deprecated Features Tests
A
B
C
D
E
F
G
H
I
J
K
end
Summary
[deprecated_test.py](/projects/286/67361) is a critical test module within the pytest codebase that enforces correct deprecation behaviors. By systematically testing various deprecated APIs and usage patterns, it helps maintain the integrity and forward compatibility of pytest. This file's tests serve as both verification and documentation of deprecated features, ensuring developers receive clear guidance and warnings when using outdated constructs.
Additional Notes
This file is mainly intended for pytest core developers and maintainers.
It covers deprecated plugin loading, hook specification and implementation, fixture and mark usage, and path argument handling.
No new features or production code is implemented here; it is purely a test suite for deprecation behaviors.
If you need further details on any specific test or pytest internals referenced here, please ask!