import pytest class ExampleClass: def get_value(self): return 10 def test_example(mocker): mocker.patch.object(ExampleClass, 'get_value', return_value = 20) obj = ExampleClass() assert obj.get_value() == 20
import pytest def test_example(mocker): mocker.patch('builtins.str', return_value='mocked_string') assert str(10) == 'mocked_string'In the above code, we import the pytest library and use the mocker object to patch the built-in str function with a mock object that returns 'mocked_string'. Finally, we call the str function with an argument 10, and it returns 'mocked_string.' In conclusion, the pytest_mock library package is a Python testing library that is used to create mock objects. The MockFixture class is one of its features that is used to create mocks for testing purposes.