Intro to Mocking: Why Unit Testing Doesn't Have To Be So Hard
summary
Shows concrete claims and working code: mocking removes dependencies, makes nondeterministic code deterministic, verifies calls with no return value, and generates exceptions on demand. Uses Python’s built‑in mock/magicmock with patch and patch.object, demonstrates side_effect and return_value to control randomness and raise errors, and uses assert_called_once_with and call_args_list to verify behavior. Examples include a Tinder‑like “docker” picker and a Stripe charge test; fixes flakiness and speeds tests while noting Django/database tradeoffs.
AI-written summary · about
chapters (14)
- 0:00Introduction
- 0:52Unit Testing
- 4:07Problems Mocks Help Solve
- 8:01Sample Problem
- 15:26Behavior Verification
- 17:46Parameter Verification
- 21:12Kitten Break
- 23:31Why Mocking Doesnt Work
- 24:52The Better Way
- 25:54Summary
- 31:19Mocking Tips
- 33:16Create AutoSpec
- 33:48Practice TDD
- 35:15Avoiding brittle tests
related talks
49mFor those about to Mock I salute you!
26mMocking: virtual reality for your Python tests
23mMocking of mocks. A deep dive into the mock module
32mMock It Till You Make It: How to Verify Your External Mocks Without Ever...
20mMock, patch - when and how to use
29mUnit testing in the real world with mock
27mI Dance with Your Shadow: Patch, Stub, Mock
31mBeyond the Mockery- Why We Should Embrace Testing Without Mocking Frameworks
