202129 Mar

How Mock Can Improve Your Unit Tests: Part 1


This two-part series of blog posts will teach you how to use Pythons unittest.mock module to add mocking to your unit tests. By using the mock as a context manager, we limit its scope to the short time we need it to be in effect. Ill usually add a comment to explain why the code is maybe a little less straightforward than a reader might expect. In the second case, the thing we want to mock isnt at the top level of the module, maybe because were importing it inside a function or class. I ended up finding a different place to mock, that wasnt being called directly from the template.

Source: Caktusgroup