Company
Date Published
Author
Mike Lin
Word count
1847
Language
English
Hacker News points
None

Summary

Revisiting unit testing and mocking in Python, the blog post discusses higher-level software engineering principles demonstrated in experience with Python testing over the past year and a half. It revisits patching mock objects in unit tests, highlighting flaws in using `patch` to test classes that use many external clients, such as creating a large number of patches for every call. The post introduces dependency inversion and dependency injection as solutions, showing how adding default values to class initialization arguments makes it easier to selectively initialize client service objects for specific tests with mocking. It also explores the use of `@property` to encapsulate lazy loading and make code more Pythonic, while preserving direct attribute access. Ultimately, the post demonstrates how using dependency inversion and dependency injection simplifies unit testing by eliminating the need for patching in favor of selectively initializing client service objects for specific tests with mocking.