~/talk

Unit testing in the real world with mock

Talkat PyCon Israel 2017 · 2017-06-11..06-13· [testing, python]
links:none on file
slides:none on file

Uses Python's unittest.mock (stdlib since 3.3, backport for 2.6+) to unit-test IO-heavy code by replacing names the module imports. Demonstrates patch.dict to fake os.environ, patch to substitute a language function and a Google Translate client, and Mock.return_value / side_effect to return specific JSON and raise errors. Verifies behavior with call counts and assert_called_with, checks random.seed and print calls, and warns a mocked API hid a real bug—advocates create_autospec plus integration tests.

AI-written summary · about

  1. 0:00Introduction to unit testing
  2. 1:00Why unit testing matters
  3. 2:25Real world testing challenges
  4. 3:53Enterprise guessing game example
  5. 5:59Testing simple calculations
  6. 7:03Mocking environment variables
  7. 9:12Introduction to mocking library
  8. 11:44Testing language-specific features
  9. 13:33Understanding mock objects
  10. 16:18Patching functions with mocks
  11. 17:21Mocking cloud API calls
  12. 20:31Testing random and print functions
  13. 21:57Verifying function calls
  14. 23:23Limitations of mocking
  15. 24:28Conclusion and next steps
  16. 27:24Q&A and closing
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.