Skip to content
Artwork for Test & Code
Test & Code · Jan 31, 2025 · 11 min

226: pytest-mock : Mocking in pytest

pytest-mock is currently the #3 pytest plugin. pytest-mock is a wrapper around unittest.mock. In this episode: Why the pytest-mock plugin is awesome What is mocking, patching, and monkey patching What, if any, is the difference between mock, fake, spy, stub. Why we might need these in testing Some history of mock in Python and how mock became unittest.mock From unittest.mock patch.object patch.object with autospec using these as context managers pytest-mock: The mocker fixture Cleanup in teardown Using mocker.patch, mocker.spy, and mocker.stub Why it's awesome and why you might want to use it over straight unittest.mock Links: top pytest plugins list pytest-mock documentation unittest.mock Podcast episode discussing unittest.mock with Michael Foord monkeypatch

0:00-11:07

transcript

No transcript — this publisher did not publish one.

show notes

pytest-mock is currently the #3 pytest plugin. 
pytest-mock is a wrapper around unittest.mock.

In this episode:

  • Why the pytest-mock plugin is awesome
  • What is mocking, patching, and monkey patching
  • What, if any, is the difference between mock, fake, spy, stub. 
  • Why we might need these in testing
  • Some history of mock in Python and how mock became unittest.mock
  • From unittest.mock
    • patch.object
    • patch.object with autospec
    • using these as context managers
  • pytest-mock:
    • The mocker fixture 
    • Cleanup in teardown
    • Using mocker.patch, mocker.spy, and mocker.stub
    • Why it's awesome and why you might want to use it over straight unittest.mock

Links:




links5