0
0
Flaskframework~10 mins

Mocking external services in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking external services
Start Test
Patch external call
Run code that calls external service
Mock returns fake response
Code uses fake response
Assert expected behavior
End Test
This flow shows how a test replaces an external service call with a fake response to test code behavior safely.
Execution Sample
Flask
from unittest.mock import patch
import requests

def get_data():
    return requests.get('https://api.example.com/data').json()
This code defines a function that fetches JSON data from an external API using requests.get.
Execution Table
StepActionEvaluationResult
1Patch requests.getrequests.get replaced by mockMock ready to intercept calls
2Call get_data()Inside get_data, requests.get calledMock intercepts call
3Mock returns fake JSONMock returns {'key': 'value'}get_data receives fake JSON
4get_data returns JSONReturns {'key': 'value'}Function output is fake data
5Assert outputCheck output == {'key': 'value'}Test passes
6Unpatch requests.getRestore original requests.getExternal calls back to normal
💡 Test ends after asserting expected behavior with mocked external call
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
requests.getoriginal functionmock functionmock functionmock functionoriginal function
get_data outputnonenone{'key': 'value'}{'key': 'value'}{'key': 'value'}
Key Moments - 2 Insights
Why does get_data return fake data instead of calling the real API?
Because requests.get is patched (replaced) by a mock that returns fake data, as shown in execution_table step 1 and 3.
What happens if we forget to unpatch requests.get after the test?
The mock stays active, so real external calls remain mocked, which can cause unexpected behavior in other tests or code, as noted in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the mock return when get_data calls requests.get?
ANone
B{'key': 'value'}
CReal API response
DAn error
💡 Hint
Check step 3 in the execution table where the mock returns fake JSON.
At which step is requests.get replaced by the mock?
AStep 1
BStep 2
CStep 4
DStep 6
💡 Hint
Check step 1 in the execution table where requests.get is replaced by mock.
If we do not unpatch requests.get, what variable_tracker value remains incorrect?
Aget_data output
Brequests.get becomes None
Crequests.get remains mock function
Dget_data output becomes None
💡 Hint
See variable_tracker row for requests.get after final step.
Concept Snapshot
Mocking external services in Flask tests:
- Use unittest.mock.patch to replace external calls
- Patch before calling code that uses the service
- Mock returns fake data to avoid real HTTP requests
- Assert code behavior with fake data
- Unpatch to restore original functions after test
Full Transcript
This lesson shows how to mock external services in Flask using unittest.mock.patch. We replace the real HTTP call with a fake one that returns controlled data. The flow starts by patching the external call, then running the code that calls it. The mock returns fake JSON data, which the code uses. We then check that the code behaves as expected with this fake data. Finally, we unpatch to restore the original external call. This prevents tests from depending on real external services and makes them faster and more reliable.