Challenge - 5 Problems
External Service Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pytest test with mocked external API call
What is the output of this pytest test when the external API call is mocked to return status code 200?
PyTest
import requests import pytest from unittest.mock import patch def fetch_data(): response = requests.get('https://api.example.com/data') if response.status_code == 200: return response.json() return None @patch('requests.get') def test_fetch_data(mock_get): mock_get.return_value.status_code = 200 mock_get.return_value.json = lambda: {'key': 'value'} result = fetch_data() assert result == {'key': 'value'} print('Test passed')
Attempts:
2 left
💡 Hint
Consider what the mock object returns for status_code and json method.
✗ Incorrect
The mock replaces requests.get to return a response with status_code 200 and a json method returning {'key': 'value'}. The assertion passes and 'Test passed' is printed.
❓ assertion
intermediate1:30remaining
Correct assertion for API response content
Which assertion correctly verifies that the API response contains a 'user' key with a non-empty dictionary?
PyTest
response = {'user': {'id': 1, 'name': 'Alice'}}Attempts:
2 left
💡 Hint
Check for key existence, type, and non-empty dictionary.
✗ Incorrect
Option A checks that 'user' key exists, is a dictionary, and is not empty. Option A only checks not None but could be empty. Option A is valid but less explicit about type. Option A asserts empty dict which is incorrect.
🔧 Debug
advanced2:00remaining
Identify the cause of test failure with external service timeout
Given this pytest test, why does it fail with a TimeoutError?
PyTest
import requests import pytest def get_data(): return requests.get('https://slow.api.example.com/data', timeout=1).json() def test_get_data(): data = get_data() assert 'result' in data
Attempts:
2 left
💡 Hint
Consider the timeout parameter in requests.get and the error type.
✗ Incorrect
The test fails because the external API response takes longer than 1 second, triggering a TimeoutError before the assertion.
❓ framework
advanced1:30remaining
Best pytest fixture scope for external service setup
Which pytest fixture scope is best to minimize external service setup overhead when running multiple tests that share the same service state?
Attempts:
2 left
💡 Hint
Think about how often you want to initialize the external service for all tests.
✗ Incorrect
Session scope runs once for the entire test session, minimizing repeated setup for external services shared across tests.
🧠 Conceptual
expert2:30remaining
Handling flaky tests caused by external service instability
Which strategy is most effective to handle flaky tests caused by intermittent failures of an external service?
Attempts:
2 left
💡 Hint
Consider reliability and automation best practices.
✗ Incorrect
Retries with backoff help handle transient failures, and mocking in CI ensures tests are stable and fast. Ignoring or manual runs reduce test coverage and automation benefits.