Mocking
The unittest.mock
module provides mocking functionality for Python testing.
The patch
function actually swaps the targeted thing for the mock object. The first argument is the thing that should be mocked and the second is optionally the thing that will replace it. We can use a Mock
or MagicMock
object.
There are a few patterns. We can use patch
as a context manager and the scoped context object can be used to assert:
We can set up complex mocks ahead of time and pass them into the context manager. This means that our asserts can be done outside of the with scope too. This setup is better for Arrange Act Assert style testing.
We can also use the @mock.patch
decorator to replace a scoped function for the duration of a test method:
If we need to stack multiple @mock.patch
annotations they are added as input arguments in reverse order because of how python decorators work.
(in the following example mock_fn3
is the mocked version of some.function3
and so on)
Mocking File Opens
As per: this SO post, patch builtins.open
and use mock_open
to wrap the calls: