Test & Code

pytest-mock : Mocking in pytest

Jan 31, 2025
Explore the wonders of mocking in Python testing with pytest-mock, a top plugin that simplifies mock management. Discover the intricacies of concepts like patching, spies, and stubs while learning about their importance in testing. Uncover the history behind unittest.mock and how it evolved in Python. The discussion also highlights the unique features of pytest-mock, including its Mocker fixture and the advantages it offers over traditional methods. Get ready to elevate your testing game!
Ask episode
AI Snips
Chapters
Transcript
Episode notes
INSIGHT

Mocking Purpose

  • Mocking replaces parts of a system with substitutes during testing.
  • This helps isolate the code being tested and avoid unwanted side effects.
INSIGHT

Test Double Types

  • Mocks, stubs, spies, and fakes represent different test doubles with varying behaviors.
  • Stubs are simple placeholders, spies track interactions, and fakes mimic real responses.
ADVICE

Using unittest.mock

  • Use unittest.mock's patch.object with or without autospec.
  • Use it as a context manager for easy cleanup.
Get the Snipd Podcast app to discover more snips from this episode
Get the app