0
0
Djangoframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Mocking external services
Test starts
Patch external service call
Run code that calls external service
Mock intercepts call and returns fake response
Test uses fake response to check behavior
Test ends
This flow shows how a test replaces an external service call with a mock, so the test controls the response and checks code behavior safely.
Execution Sample
Django
from unittest.mock import patch

def test_fetch_data():
    with patch('myapp.services.requests.get') as mock_get:
        mock_get.return_value.status_code = 200
        mock_get.return_value.json.return_value = {'key': 'value'}
        result = fetch_data()
        assert result == {'key': 'value'}
This test replaces the real HTTP GET call with a mock that returns a preset response, then checks the function output.
Execution Table
StepActionMock StateFunction CallReturned ValueTest Assertion
1Patch 'requests.get' with mock_getmock_get ready, no calls yetNo call yetN/AN/A
2Call fetch_data(), which calls requests.getmock_get called oncerequests.get() calledMock response with status 200 and JSON {'key': 'value'}N/A
3fetch_data() processes mock responsemock_get still called onceN/A{'key': 'value'}N/A
4Test asserts result equals {'key': 'value'}mock_get still called onceN/A{'key': 'value'}Pass
5Test ends, patch removedmock_get removedN/AN/AN/A
💡 Test ends after assertion passes using mocked external service response
Variable Tracker
VariableStartAfter Step 2After Step 3Final
mock_get.call_count0111
mock_get.return_value.status_codenull200200200
mock_get.return_value.json.return_valuenull{'key': 'value'}{'key': 'value'}{'key': 'value'}
resultnullnull{'key': 'value'}{'key': 'value'}
Key Moments - 3 Insights
Why does the test not make a real HTTP request?
Because the 'requests.get' function is replaced by a mock (see Step 1 and 2 in execution_table), so the real network call is never made.
How does the mock know what to return when called?
We set 'mock_get.return_value.status_code' and 'mock_get.return_value.json.return_value' before calling the function (Step 2), so the mock returns these values.
What happens if the function calls 'requests.get' multiple times?
The mock tracks each call in 'mock_get.call_count' (see variable_tracker), so you can check how many times it was called and control responses accordingly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the mock_get.call_count value?
A2
B0
C1
Dnull
💡 Hint
Check the 'Mock State' column at Step 2 in the execution_table.
At which step does the test check the function's returned value?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look for 'Test asserts result equals' in the 'Action' column of the execution_table.
If we did not patch 'requests.get', what would happen during Step 2?
AThe test would call the real external service
BThe mock would still intercept the call
CThe test would fail immediately
DThe function would return null
💡 Hint
Refer to the 'Action' and 'Mock State' columns in the execution_table and the key moment about patching.
Concept Snapshot
Mocking external services in Django tests:
- Use unittest.mock.patch to replace external calls
- Set mock return values before calling your code
- Your code calls the mock, not the real service
- Assert your code handles the mock response correctly
- Patch is removed after test ends
Full Transcript
This visual execution shows how to mock external services in Django tests using unittest.mock.patch. The test replaces the real HTTP request function with a mock object. The mock is set up to return a fake response with a status code and JSON data. When the tested function calls the external service, it receives the mock response instead of making a real network call. The test then asserts that the function returns the expected data from the mock. This approach avoids slow or unreliable real service calls during testing and lets you control test scenarios precisely.