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"}

Fixture: my_fixture

@pytest.fixture(scope="function")
@mock.patch.dict(config, {"mykey": "MOCKED"})
def my_fixture():
    return config["mykey"]

Test Function: test_foobar

def test_foobar(my_fixture):
    assert my_fixture == "MOCKED"

Important Implementation Details


Interaction with Other Parts of the System


Summary


Mermaid Class Diagram

classDiagram
    class fixture_mock_integration {
        +config: dict
        +my_fixture() str
        +test_foobar(my_fixture) void
    }

End of Documentation for fixture_mock_integration.py