def test_my_func(mocker: MockFixture): mocker.patch('my_module.my_func', return_value=42) assert my_module.my_func() == 42
def test_my_class(mocker: MockFixture): my_mock = mocker.Mock() my_mock.get_data.return_value = 'test data' mocker.patch.object(MyClass, 'my_method', return_value=my_mock) my_obj = MyClass() assert my_obj.my_method().get_data() == 'test data'In this example, we create a new `Mock` object and set its `get_data` method to return `'test data'`. We then use `mocker.patch.object` to stub the `my_method` method of `MyClass` to always return our mock object. This allows us to test how our code interacts with the `get_data` method of the mock object in a controlled environment. The package library being used here is `pytest-mock`, which extends the standard `unittest.mock` library with additional functionality and a more convenient API specifically for use within the pytest framework.