Using Plugins to Assist Mocking
This chapter has focused on using unittest.mock directly. However, there are many plugins that help with mocking, such as pytest-mock, which is a general-purpose plugin that provides a mocker fixture that acts as a thin wrapper around unittest.mock. One benefit of using pytest-mock is that the fixture cleans up after itself, so you don’t have to use a with block, as we did in our examples.
There are also several special-purpose mocking libraries that should be considered if their focus matches what you are testing:
-
For mocking database access, try pytest-postgresql, pytest-mongo, pytest-mysql, and pytest-dynamodb.
-
For mocking HTTP servers, try pytest-httpserver.
-
For mocking requests, try responses and betamax ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access