fixture_mock_integration.py
Overview
This file demonstrates a minimal example to reproduce a specific issue (referenced as issue #3774) involving the use of `unittest.mock.patch.dict` within a pytest fixture. It showcases how to temporarily modify a dictionary (`config`) during a test run using patching, and verifies that the patched value is correctly applied in the test function.
The file primarily serves as a test case highlighting an interaction between pytest fixtures and dictionary patching, ensuring the patched dictionary values are visible within the fixture and test scope.
Detailed Explanation
Module-level Variable
config = {"mykey": "ORIGINAL"}
A global dictionary named
configinitialized with one key-value pair"mykey": "ORIGINAL".This dictionary simulates a configuration or environment that might be patched during testing.
Fixture: my_fixture
@pytest.fixture(scope="function")
@mock.patch.dict(config, {"mykey": "MOCKED"})
def my_fixture():
return config["mykey"]
Purpose:
Provides a pytest fixture that yields the patched value ofconfig["mykey"].Decorators:
@pytest.fixture(scope="function"):
Declaresmy_fixtureas a pytest fixture with function scope, meaning it is set up and torn down for each test function invocation.@mock.patch.dict(config, {"mykey": "MOCKED"}):
Usesunittest.mock.patch.dictto temporarily update theconfigdictionary by replacing"mykey"'s value with"MOCKED"during the lifetime of the fixture function.
Functionality:
Whenmy_fixtureis used in a test, theconfigdictionary is patched, and the fixture returns the patched value"MOCKED"fromconfig["mykey"].Parameters:
None.Returns:
The string"MOCKED", which is the patched value ofconfig["mykey"].Usage Example:
def test_example(my_fixture): assert my_fixture == "MOCKED"
Test Function: test_foobar
def test_foobar(my_fixture):
assert my_fixture == "MOCKED"
Purpose:
A pytest test function that consumesmy_fixtureand asserts that the patched dictionary value is correctly applied.Parameters:
my_fixture(fixture): Injected by pytest, expected to be the patched value"MOCKED".
Behavior:
The test passes ifmy_fixtureequals"MOCKED", verifying that the patching within the fixture operates as expected.Returns:
None (raises assertion error if the test fails).
Important Implementation Details
The use of
mock.patch.dictas a decorator on the fixture function is key. This approach patches the globalconfigdictionary only during the fixture execution. After the fixture completes, the patch is undone, andconfigreturns to its original state.The order of decorators is important:
@mock.patch.dictmust wrap the function to ensure patching is applied inside the fixture's scope.The test demonstrates that patching a dictionary in this manner works seamlessly with pytest fixtures, ensuring isolated and repeatable test environments.
The
# mypy: allow-untyped-defscomment at the top disables mypy warnings about missing type annotations, which is common in test files for brevity.
Interaction with Other Parts of the System
This file is a standalone test module focusing on reproducing a particular patching behavior issue. It interacts with:
pytest:
For fixture and test discovery and execution.unittest.mock:
For patching the dictionary in a controlled test scope.
The
configdictionary could be representative of a configuration module or settings object in a larger application. This test pattern could be used to mock configuration values during unit testing without permanent side effects.
Summary
Defines a global dictionary
config.Creates a pytest fixture
my_fixturethat temporarily patchesconfig["mykey"]to"MOCKED".Tests that the fixture correctly returns the patched value.
Demonstrates effective use of
mock.patch.dictwith pytest fixtures to isolate test state.
Mermaid Class Diagram
classDiagram
class fixture_mock_integration {
+config: dict
+my_fixture() str
+test_foobar(my_fixture) void
}
configis a global dictionary.my_fixtureis a fixture function returning a string.test_foobaris a test function acceptingmy_fixture.