Q57: How does unittest.mock.patch work when mocking dependencies?
unittest.mock.patch replaces target objects with MagicMock instances during testing scope. Crucial Rule: Always patch the target reference where it is imported/used, NOT where it is defined.
Key Takeaways
- Replaces dependencies with
MagicMockobjects. - Patch where the module IS USED, not where it is defined.
Knowledge Check
If module_a.py imports 'requests', where should you target patch() inside test scripts testing module_a?