integration.py
Overview
The `integration.py` file is a test suite primarily focused on verifying the behavior and integration of mocking and patching features within the `pytest` testing framework. It contains a collection of test cases that ensure compatibility and correct functionality when using various mocking utilities such as `unittest.mock`, the third-party `mock` package, and `pytest` fixtures.
This file tests complex scenarios involving wrapped functions, mock decorators, interaction of mocks with fixtures, parameterization of tests, and correct behavior of test collection and rerun mechanisms. It also verifies that pytest's internal utilities for function inspection unwrap decorated or patched functions properly.
The tests target the integration points of pytest's test collection, execution, and mocking support, helping maintain robust and predictable behavior when users combine these features in practical test suites.
Classes, Functions, and Methods
Functions
test_wrapped_getfslineno() -> None
Purpose:
Tests the _pytest._code.getfslineno function's ability to correctly unwrap decorated functions to find the original source file and line number.Details:
Defines a wrapped function with a wrapped attribute and asserts thatgetfslinenoreturns line numbers reflecting the unwrapping.Usage Example:
test_wrapped_getfslineno()Return:
None (raises assertion if unwrapping fails)
Class: TestMockDecoration
A collection of tests focused on verifying that pytest interacts correctly with mock decorators and patched functions.
Methods:
test_wrapped_getfuncargnames(self) -> None
Tests thatgetfuncargnamesfrom_pytest.compatcorrectly retrieves argument names from wrapped functions.test_getfuncargnames_patching(self) -> None
Verifies argument detection on functions decorated withunittest.mock.patch.test_unittest_mock(self, pytester: Pytester) -> None
Checks that a test usingunittest.mock.patchruns successfully under pytest.test_unittest_mock_and_fixture(self, pytester: Pytester) -> None
Tests coexistence ofunittest.mock.patchand pytest fixtures in a test function.test_unittest_mock_and_pypi_mock(self, pytester: Pytester) -> None
Ensures compatibility when using bothunittest.mockand the third-partymockpackage decorators in the same test class.test_mock_sentinel_check_against_numpy_like(self, pytester: Pytester) -> None
Validates that mock detection uses identity comparison to avoid errors with objects like numpy arrays that raise exceptions on equality checks.test_mock(self, pytester: Pytester) -> None
Tests nested patch decorators from themockpackage and their effect on function calls and return values.test_mock_sorting(self, pytester: Pytester) -> None
Checks that tests decorated with mocks are run in the defined order.test_mock_double_patch_issue473(self, pytester: Pytester) -> None
Verifies that double patching with multiple decorators on a test class works correctly.
Class: TestReRunTests
Tests related to pytest's rerun functionality.
test_rerun(self, pytester: Pytester) -> None
Creates a conftest file to rerun tests twice by invokingruntestprotocoltwice inpytest_runtest_protocolhook, verifying that fixtures are properly re-instantiated on reruns.
Function: test_pytestconfig_is_session_scoped() -> None
Purpose:
Confirms that the internal pytest fixturepytestconfigis marked as session-scoped.
Class: TestNoselikeTestAttribute
Tests the behavior of the `__test__` attribute that controls whether modules, classes, or functions are considered test items by pytest.
test_module_with_global_test(self, pytester: Pytester) -> None
Ensures modules with__test__ = Falseare not collected as tests.test_class_and_method(self, pytester: Pytester) -> None
Checks that classes or methods with__test__set to False are ignored.test_unittest_class(self, pytester: Pytester) -> None
Verifies that unittest.TestCase classes with__test__ = Falseare skipped.test_class_with_nasty_getattr(self, pytester: Pytester) -> None
Tests that classes with dynamic__getattr__implementations do not confuse pytest's test detection logic.
Class: TestParameterize
Tests related to pytest's parameterization features.
test_idfn_marker(self, pytester: Pytester) -> None
Tests custom ID functions (idfn) for parameterized test markers.test_idfn_fixture(self, pytester: Pytester) -> None
Tests custom ID functions for parameterized fixtures.test_param_rejects_usefixtures(self, pytester: Pytester) -> None
Ensures thatpytest.paramrejects usage withusefixturesmarks, which is invalid.
Function: test_function_instance(pytester: Pytester) -> None
Purpose:
Validates that pytest'sFunctiontest items correctly associate instances with test methods, including class, static, and instance methods.Details:
Creates test functions and methods in a sample module and asserts that the pytestFunctionobjects have the expectedinstanceattributes.
Important Implementation Details and Algorithms
Function Unwrapping:
The file tests the ability of pytest internal utility functions likegetfslinenoandgetfuncargnamesto unwrap decorated or patched functions to the original callable to correctly identify source lines and argument names.Mock and Patch Integration:
Many tests exercise the interaction between pytest's fixture system and mock decorators from both the standard library and third-partymockpackage, ensuring mocks do not break fixture injection or test discovery.Identity Comparison for Mock Sentinels:
To avoid issues with objects like numpy arrays that raise exceptions on equality checks, the detection of mock sentinel objects uses identity (is) checks instead of equality (==).Multiple Reruns:
The rerun test demonstrates how to programmatically rerun tests multiple times within the same test session by manually invokingruntestprotocoltwice.Test Collection Filtering:
Tests verify that pytest respects the__test__attribute to skip modules, classes, or methods marked as not tests, including special handling for unittest.TestCase subclasses.
Interaction with Other System Components
Pytest Internals:
The tests depend heavily on internal pytest modules such as_pytest._code,_pytest.fixtures,_pytest.compat, and_pytest.python.Function. They validate core pytest features like test collection, fixture injection, and test execution.Mocking Libraries:
Integration tests useunittest.mockand the externalmockpackage to simulate patched environments, verifying that pytest correctly discovers and runs tests decorated with these mocks.Pytester Plugin:
ThePytesterfixture is extensively used to create temporary test files, run pytest sessions, and inspect results programmatically, enabling testing of pytest behavior in a controlled environment.
Usage Example
The file itself is not intended to be imported as a module but run as a pytest test suite to verify pytest's integration with mocks and decorators. To execute the tests:
pytest integration.py
Diagram: Class Diagram of Test Classes and Methods
classDiagram
class TestMockDecoration {
+test_wrapped_getfuncargnames()
+test_getfuncargnames_patching()
+test_unittest_mock(pytester)
+test_unittest_mock_and_fixture(pytester)
+test_unittest_mock_and_pypi_mock(pytester)
+test_mock_sentinel_check_against_numpy_like(pytester)
+test_mock(pytester)
+test_mock_sorting(pytester)
+test_mock_double_patch_issue473(pytester)
}
class TestReRunTests {
+test_rerun(pytester)
}
class TestNoselikeTestAttribute {
+test_module_with_global_test(pytester)
+test_class_and_method(pytester)
+test_unittest_class(pytester)
+test_class_with_nasty_getattr(pytester)
}
class TestParameterize {
+test_idfn_marker(pytester)
+test_idfn_fixture(pytester)
+test_param_rejects_usefixtures(pytester)
}
TestMockDecoration <|-- integration.py
TestReRunTests <|-- integration.py
TestNoselikeTestAttribute <|-- integration.py
TestParameterize <|-- integration.py
Summary
`integration.py` is a comprehensive test suite for pytest's integration with mocking frameworks and test parameterization. It ensures that pytest can correctly unwrap decorated functions, handle mock patches, respect test-disabling attributes, and maintain fixture and test execution correctness across reruns and complex decoration scenarios. This file acts as a critical safeguard for pytest's interoperability with mocking utilities and advanced test features.