

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!
AI Snips
Chapters
Transcript
Episode notes
Mocking Purpose
- Mocking replaces parts of a system with substitutes during testing.
- This helps isolate the code being tested and avoid unwanted side effects.
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.
Using unittest.mock
- Use
unittest.mock
'spatch.object
with or withoutautospec
. - Use it as a context manager for easy cleanup.