Unit tests using mock library

The two tests we wrote earlier were pretty straightforward to implement. Often, it is not trivial to write a test for verifying the functionality. The reasons could vary. In some scenarios, the code is required to be refactored in order to access the functionality you would like to test. In another scenario, the code might have dependencies that require you to write a lot more code than necessary. It is also possible that the functionality to be tested needs time consuming preparatory work such as crunching some numbers. This adds to the total test execution time. We will now learn how to write a unit test in such situations using the mock library. Before working on the actual code, let's understand what functionality ...

Get Learning Python Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.