Mocking: virtual reality for your Python tests
summary
Shows how to replace external SMS, API and filesystem calls with Python’s unittest.mock / mock objects, walks through using side_effect and return_value to simulate errors and responses, and demonstrates patch where lookup happens (module vs imported name) plus MagicMock, mock_open, spec and wraps. Examples are concrete: an SMS router and a mobile midwife app, tests that avoid network/files, and patterns to avoid over-mocking. Practical, example-driven and useful if tests touch I/O or external services.
AI-written summary · about
chapters (12)
- 0:00Intro
- 4:17What is mock
- 5:25How to use mock
- 7:09Return value
- 8:47Deep mocking
- 10:12Magic Mach
- 11:13Magic Mark
- 13:01Mocking a module
- 18:09Spec
- 20:21Wraps
- 21:31Wrapping up
- 22:47Questions
related talks
49mFor those about to Mock I salute you!
29mUnit testing in the real world with mock
32mMock It Till You Make It: How to Verify Your External Mocks Without Ever...
39mIntro to Mocking: Why Unit Testing Doesn't Have To Be So Hard
20mMock, patch - when and how to use
24mTesting Your Python Code
23mMocking of mocks. A deep dive into the mock module
02h 13mPython Unit Testing with Pytest and Mock
