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

Summary

The text discusses the basics of mocking in Python using the unittest.mock module. Mocking is defined as replacing one or more function calls or objects with mock calls or objects, allowing for easy testing of failures and exception handling. The patch decorator can be used to hijack API function or object creation calls, returning a MagicMock object by default. This allows developers to control the behavior of the patched call, including setting return values and raising exceptions. The side_effect attribute can be used to test that functions correctly handle exceptions. Mocking is crucial for writing self-contained tests with no dependencies, allowing developers to focus on testing code functionality rather than setup.