import pytest @pytest.fixture def my_mock(): with pytest_mock.mock() as m: yield m def test_my_mock(my_mock): my_mock.some_mock_method.return_value = 42 assert my_mock.some_mock_method() == 42
from unittest.mock import MagicMock def test_my_function(): mock_obj = MagicMock() mock_obj.my_method.return_value = "mocked result" assert my_function(mock_obj) == "mocked result"In this example, we create a MagicMock object named "mock_obj". We then set the return value of its "my_method" method to "mocked result". We then call the "my_function" function with the mock object as an argument and assert that it returns the expected value. Both examples use pytest_mock and MagicMock to create and use mock objects in tests.