Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mock out dependencies while testing in Python
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Rain
March 19, 2021
0
190
Mock out dependencies while testing in Python
Rain
March 19, 2021
Tweet
Share
More Decks by Rain
See All by Rain
System Observability: We can improve only what we can observe
rainrainwu
0
69
Handling a High Performance PostgreSQL Database
rainrainwu
0
26
Scheduling Async Tasks with Python Celery
rainrainwu
0
70
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Utilizing Notion as your number one productivity tool
mfonobong
4
270
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
140
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
AI: The stuff that nobody shows you
jnunemaker
PRO
4
500
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
10k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Speed Design
sergeychernyshev
33
1.6k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
280
Building Adaptive Systems
keathley
44
3k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
150
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