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 the main challenge when testing code that interacts with external services?
The main challenge is that external services can be unreliable, slow, or change unexpectedly, which can cause tests to fail even if the code is correct.
Click to reveal answer
beginner
Why do we use mocking in tests involving external services?
Mocking replaces the real external service with a fake one that returns controlled responses, making tests faster, reliable, and independent of the real service.
Click to reveal answer
intermediate
What pytest feature helps to replace parts of your system during tests?
Pytest supports fixtures and the 'monkeypatch' fixture to replace functions or objects temporarily during a test.
Click to reveal answer
intermediate
How can you simulate different responses from an external API in pytest?
You can create a mock function that returns different data or raises exceptions, then use monkeypatch to replace the real API call with this mock during tests.
Click to reveal answer
advanced
What is a good practice to ensure tests with external services remain fast and reliable?
Use mocking or stubbing to avoid real network calls, and test integration with real services separately in dedicated integration tests.
Click to reveal answer
Why should tests avoid calling real external services directly?
ABecause real services always return errors
BBecause real services can be slow or unavailable, causing flaky tests
CBecause tests must always run offline
DBecause external services are illegal to use in tests
✗ Incorrect
Real external services can be slow, unreliable, or change unexpectedly, which makes tests flaky and slow.
What does mocking an external service mean in testing?
AReplacing the real service with a fake one that returns controlled data
BDeleting the external service
CRunning the external service on your local machine
DIgnoring the external service calls
✗ Incorrect
Mocking means replacing the real service with a fake version that returns predictable responses for testing.
Which pytest feature helps you replace functions or objects during a test?
Aparametrize decorator
Bassert statement
Cmonkeypatch fixture
Dskip marker
✗ Incorrect
The monkeypatch fixture allows you to replace or modify objects temporarily during a test.
What is a benefit of using mocking in tests with external services?
ATests run faster and are more reliable
BTests become more complex
CTests require internet connection
DTests always fail
✗ Incorrect
Mocking avoids real network calls, making tests faster and less likely to fail due to external issues.
When should you test with the real external service instead of mocking?
AOnly when the service is down
BNever, mocking is always better
COnly for unit tests
DIn dedicated integration tests to verify real interactions
✗ Incorrect
Integration tests check real interactions with external services to ensure everything works end-to-end.
Explain why mocking external services is important in automated tests.
Think about what happens if your test depends on the internet or a service that might be down.
You got /4 concepts.
Describe how you would use pytest to mock an external API call in a test.
Focus on how pytest's monkeypatch can help replace parts of your code during tests.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to use mocking when testing with external services in pytest?
easy
A. To avoid calling the real external service and make tests faster
B. To increase the number of real API calls during testing
C. To make tests dependent on internet speed
D. To test the external service itself
Solution
Step 1: Understand the role of mocking in tests
Mocking replaces real external calls with fake ones to avoid delays and failures.
Step 2: Identify the benefit of mocking external services
Mocking makes tests faster and more reliable by not depending on real services.
Final Answer:
To avoid calling the real external service and make tests faster -> Option A
Quick Check:
Mocking speeds up tests by faking external calls [OK]
Hint: Mock external calls to speed tests and avoid failures [OK]
Common Mistakes:
Thinking mocking increases real API calls
Believing tests should depend on internet speed
Confusing testing external service with testing your code
2. Which of the following is the correct way to mock a function get_data from module external_api using pytest's patch decorator?
easy
A. @patch('external_api.get_data')
B. @patch('get_data.external_api')
C. @patch('external_api->get_data')
D. @patch('external_api.getData')
Solution
Step 1: Recall correct patch syntax
The patch decorator requires the full import path as a string: 'module.function'.
Step 2: Match the correct option
@patch('external_api.get_data') uses 'external_api.get_data' which is the correct format and case-sensitive.
Final Answer:
@patch('external_api.get_data') -> Option A
Quick Check:
patch('module.function') syntax is correct [OK]
Hint: Use 'module.function' string in patch decorator [OK]
Common Mistakes:
Swapping module and function order
Using wrong separators like '->'
Incorrect function name casing
3. Given the following pytest test code, what will be the output when running the test?
The assertion should use result['status'] instead of result.status -> Option B
Quick Check:
Dict keys need brackets, not dot notation [OK]
Hint: Use brackets for dict keys in assertions [OK]
Common Mistakes:
Using dot notation on dicts
Forgetting parentheses in patch decorator (not here)
Expecting return_value must be string always
5. You want to test a function process_data() that calls an external API fetch_data(). The API sometimes returns null. How should you mock fetch_data in pytest to test process_data handles null correctly?
hard
A. Do not mock fetch_data; test process_data only with real data
B. Call the real fetch_data to see if it returns null
C. Mock fetch_data to always raise an exception
D. Use patch to make fetch_data return null and assert process_data handles it
Solution
Step 1: Understand the need to test null handling
Since fetch_data can return null, tests must simulate this to check process_data behavior.
Step 2: Use patch to mock fetch_data returning null
Mocking fetch_data to return null allows controlled testing of process_data's handling of that case.
Final Answer:
Use patch to make fetch_data return null and assert process_data handles it -> Option D
Quick Check:
Mock null return to test edge case handling [OK]
Hint: Mock edge cases like null to test error handling [OK]