On this page
article
Mock External API
Patch HTTP calls in unit tests.
Category: unittest.mock
Problem
Patch HTTP calls in unit tests.
Solution
@patch("app.requests.get")
def test_fetch(mock_get):
mock_get.return_value.json.return_value = {"ok": True}
Notes
- Adapt variable names and paths to your project
- Add error handling for production use
- See related chapters in the Learning Path