We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode pytest-mock : Mocking in pytest

pytest-mock : Mocking in pytest

2025/1/31
logo of podcast Test & Code

Test & Code

AI Chapters Transcript
Chapters
This chapter introduces the core concepts of mocking, patching, and monkey patching in software testing, explaining their purposes and relationships. It also briefly touches upon the differences between mocks, stubs, spies, and fakes.
  • Mocking involves replacing parts of a system with substitutes for testing purposes.
  • Patching is the act of replacing parts of a system with mocks.
  • Monkey patching is a simpler form of patching.
  • Mocks, stubs, spies, and fakes are related concepts with overlapping definitions.

Shownotes Transcript

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:

** Learn pytest**