Mocking: virtual reality for your Python tests
Unit tests: they are fast, easy to write and protect your code against future bugs. But what happens when unit tests depend on modules you don't want to test? Or even when they depend on external APIs you cannot use while testing? That's where mocking comes in. The
unittest.mock module lets you mimic behavior for your test dependencies. It also gives you tools to make sure your code calls its dependencies at the right places and with the right arguments.
This talk will use a set of examples to showcase what the
mock module can do, and how to make use of it in your tests. We will cover mocking functions, objects and modules, as well as various caveats and recipes for mocking Python code.
Rapha discovered Python in high school and never looked back. He has used Python for academic research, machine learning and more recently web development. After working for startups in California, he moved to Timor-Leste where he works on aid projects for Catalpa International. He is a Django contributor and a general open source enthusiast. When the Timorese Internet goes down, you can find him freediving and practicing his broken Tetun.