Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Mock out dependencies while testing in Python
Search
Rain
March 19, 2021
210
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mock out dependencies while testing in Python
Rain
March 19, 2021
More Decks by Rain
See All by Rain
System Observability: We can improve only what we can observe
rainrainwu
0
73
Handling a High Performance PostgreSQL Database
rainrainwu
0
34
Scheduling Async Tasks with Python Celery
rainrainwu
0
82
Featured
See All Featured
How Software Deployment tools have changed in the past 20 years
geshan
2
34k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
510
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
850
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.2k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Producing Creativity
orderedlist
PRO
348
41k
Rails Girls Zürich Keynote
gr2m
96
14k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
560
Unsuck your backbone
ammeep
672
58k
A better future with KSS
kneath
240
18k
Transcript
Mock Out External Dependencies Rain Wu
Agenda Why isolate dependencies during testing The scope of patched
object The potential risks of mocking dependencies with mock package The tricks of MagickMock
Why isolate dependencies during testing The essence of the test
Tragedies while testing without isolation When should I mock?
foo.py test_foo.py
foo.py test_foo.py Function Test Determines Reflect
foo.py test_foo.py Execute the logic Check the result Call Return
None
Sometimes you should not really execute the logic during the
test
Otherwise… Paid for your API calls Got banned because high-frequency
access Pollute the storage Execute unrecoverable operations
Lab 01 How py te st po ll u te
your s to rage while unit- te s ti ng?
None
The scope of patched object Context manager Function decorator Class
decorator Construct and destruct manually (not recommend)
Lab 02 - 1 Con te xt manager
Lab 02 - 2 Func ti on decora to r
Lab 02 - 3 Class decora to r
Construct and Destruct Manually
The Potential Risks of Mocking Dependencies Wrong parameters Misspelling methods
Lab 03 - 1 Th e unit te st was
passed accidenta ll y (but maybe you th ink th at it should be like th is )
Lab 03 - 2 In some cases, mock wi ll
catch th e issues for you au to ma ti ca ll y
Lab 03 - 3 But some ti mes it won’t,
so we had be tt er make th e schema of patched object s tr ic te r by ourselves
The Tricks of MagicMock Object attributes
Lab 04 - 1 Th e valid a tt ribu
te s of a MagicMock object
Lab 04 - 2 Apply th e fi xed a
tt ribu te s recursively via th e au to spec kwarg
Thanks