def test_mock_func(mocker): mock_function = mocker.Mock(return_value=42) assert mock_function() == 42
class MyClass: def my_method(self, a, b): pass def test_autospec(mocker): mock_object = mocker.create_autospec(MyClass) mock_object.my_method(1, 2) mock_object.my_method.assert_called_once_with(1, 2)In this example, we create a class `MyClass` with a single method `my_method`. We then use `create_autospec` to create a mock object `mock_object` that has the same interface as `MyClass`. We call the `my_method` method on `mock_object` with the arguments `(1, 2)` and assert that the method was called with the expected arguments. In conclusion, `pytest_mock` is a package library that provides additional functionalities to the Python testing framework called `pytest`. It extends the `unittest.mock` library and provides a `MockFixture` fixture that provides the ability to create and manage mock objects. Additionally, `unittest.mock` provides `create_autospec` function that allows for generating mock objects based on a given function or class.