~/talk

Demystifying the Patch Function

Talkat PyCon 2018 · on Friday, 2018-05-11· [python, testing]
github:lisroach
slides:none on file

Explains exactly how unittest.mock.patch replaces a target by name (examples with my_module.db_write and from-import vs import cases), when to use context manager, function or class decorator or manual start/stop, and how to avoid false positives with spec / autospec / spec_set. Covers new_callable, new, create and useful kwargs, demonstrates mocking DB writes and builtins, and warns about leaks and autospec introspection side effects so a viewer knows if they need concrete examples and gotchas or can skip it.

AI-written summary · about

  1. 0:00Start
  2. 1:42What does Patch do?
  3. 2:54When should you mock?
  4. 6:03What makes it so confusing?
  5. 6:42Target Must be Importable
  6. 7:38my_module.py
  7. 10:39Scope Matters
  8. 12:11Context Manager
  9. 13:14Function Decorator
  10. 14:54Manual Start/Stop
  11. 16:54spec & autospec & spec_set
  12. 17:14Mock Problems
  13. 18:45A MagicMock
  14. 19:29A Specced MagicMock
  15. 23:42new_callable & new
  16. 25:38create
  17. 26:25kwargs
  18. 27:33Takeaways
~/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.