Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is mocking in the context of testing external services?
Mocking means creating a fake version of an external service to test your code without calling the real service. It helps test how your code behaves when the service responds in certain ways.
Click to reveal answer
beginner
Which Python library is commonly used in Django to mock external HTTP requests?
The unittest.mock library is commonly used to replace parts of your code with mock objects. For HTTP requests, libraries like requests-mock or patching requests.get with unittest.mock are popular.
Click to reveal answer
beginner
Why should you mock external services in Django tests?
Mocking avoids slow or unreliable tests caused by real external services. It also prevents using real data or costs, and lets you test how your code handles different responses or errors.
Click to reveal answer
intermediate
How do you use patch from unittest.mock to mock an external API call in Django?
You use @patch('path.to.external.call') above your test function to replace the real call with a mock. Inside the test, you set what the mock should return or raise to simulate the external service.
Click to reveal answer
intermediate
What is a common pattern to test error handling when mocking external services?
You configure the mock to raise an exception or return an error response. Then you check if your code handles it gracefully, like retrying, logging, or returning a fallback result.
Click to reveal answer
What does mocking an external service help you avoid in tests?
ARunning tests locally
BCalling the real external service
CUsing Django models
DWriting any test code
✗ Incorrect
Mocking replaces the real external service call with a fake one, so tests don't call the real service.
Which Python module provides the patch decorator for mocking?
Arequests
Bdjango.test
Cunittest.mock
Dpytest
✗ Incorrect
unittest.mock is the standard Python module for mocking, including the patch decorator.
When mocking an external API call, what can you set on the mock object?
AUser sessions
BDatabase connections
CCSS styles
DReturn values and exceptions
✗ Incorrect
You can set what the mock returns or what exceptions it raises to simulate different API responses.
Why is it important to test error handling with mocks?
ATo ensure your code handles failures gracefully
BTo speed up the real API
CTo avoid writing tests
DTo change the database schema
✗ Incorrect
Testing error handling ensures your code reacts properly when the external service fails.
Which of these is NOT a benefit of mocking external services?
ATesting UI layout
BFaster tests
CAvoiding real service costs
DControlling test scenarios
✗ Incorrect
Mocking helps with backend service calls, not UI layout testing.
Explain how you would mock an external HTTP request in a Django test using unittest.mock.
Think about replacing the real call with a fake one that returns controlled data.
You got /4 concepts.
Describe why mocking external services is important for reliable and fast Django tests.
Consider what happens if your tests depended on real internet calls.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to use mocking for external services in Django tests?
easy
A. To avoid making real network calls during tests
B. To speed up the Django development server
C. To automatically generate API documentation
D. To deploy the Django app faster
Solution
Step 1: Understand the purpose of mocking
Mocking replaces real external calls with fake ones to avoid delays and failures during tests.
Step 2: Identify the benefit in testing context
By not making real network calls, tests run faster and are more reliable.
Final Answer:
To avoid making real network calls during tests -> Option A
Quick Check:
Mocking avoids real calls = A [OK]
Hint: Mock external calls to keep tests fast and reliable [OK]
Common Mistakes:
Thinking mocking speeds up the server
Confusing mocking with deployment
Assuming mocking generates docs
2. Which of the following is the correct way to patch an external service call in a Django test using unittest.mock?
easy
A. @patch('services.external_api_call.myapp')
B. @patch('external_api_call.myapp.services')
C. @patch('myapp.external_api_call.services')
D. @patch('myapp.services.external_api_call')
Solution
Step 1: Understand patch target format
The patch decorator requires the full import path to the function or method to mock.
Step 2: Match the correct import path
@patch('myapp.services.external_api_call') correctly specifies the module and function as 'myapp.services.external_api_call'.
Final Answer:
@patch('myapp.services.external_api_call') -> Option D
Quick Check:
Correct patch path = D [OK]
Hint: Patch using full import path of the function to mock [OK]
Common Mistakes:
Reversing module and function order
Using incomplete import paths
Patching the wrong module
3. Given this test code snippet, what will be printed?
from unittest.mock import patch
def get_data():
return external_service_call()
@patch('myapp.services.external_service_call')
def test_get_data(mock_call):
mock_call.return_value = {'status': 'ok'}
result = get_data()
print(result)
medium
A. Error: external_service_call not defined
B. None
C. external_service_call
D. {'status': 'ok'}
Solution
Step 1: Understand patch effect on external_service_call
The patch replaces external_service_call with a mock that returns {'status': 'ok'} only inside the decorated function test_get_data.
Step 2: Analyze get_data call outside patch scope
get_data() calls external_service_call(), but external_service_call is not defined globally, leading to a NameError.
Final Answer:
Error: external_service_call not defined -> Option A
Quick Check:
external_service_call undefined outside patch = D [OK]
Hint: Patch only affects references inside the decorated function [OK]
Common Mistakes:
Assuming patch affects global scope
Expecting mock return_value outside patch
Ignoring NameError due to missing definition
4. What is wrong with this test code that tries to mock an external API call?
from unittest.mock import patch
def test_api_call():
with patch('myapp.services.external_api_call') as mock_call:
mock_call.return_value = {'success': True}
result = external_api_call()
print(result)
medium
A. external_api_call cannot be patched with patch()
B. The patch context ends before calling external_api_call
C. mock_call.return_value should be set after calling external_api_call
D. Missing import for external_api_call
Solution
Step 1: Check patch context usage
The patch is applied only inside the with block, but external_api_call is called after it ends.
Step 2: Understand effect on mocking
Since external_api_call is called outside the patch block, it is not mocked and runs the real function.
Final Answer:
The patch context ends before calling external_api_call -> Option B
Quick Check:
Call must be inside patch block = C [OK]
Hint: Call mocked function inside patch block or decorator [OK]
Common Mistakes:
Calling function outside patch context
Setting return_value too late
Assuming patch works globally without context
5. You want to test a Django view that calls an external payment API. Which approach correctly mocks the external call and verifies the view handles a failure response gracefully?