0
0
PyTesttesting~20 mins

Testing with external services in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
External Service Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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')
ATest passed
BTypeError
CAssertionError
DNo output
Attempts:
2 left
💡 Hint
Consider what the mock object returns for status_code and json method.
assertion
intermediate
1: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'}}
Aassert 'user' in response and isinstance(response['user'], dict) and response['user']
Bassert response['user'] != None
Cassert response['user'] is not None and len(response['user']) > 0
Dassert response.get('user') == {}
Attempts:
2 left
💡 Hint
Check for key existence, type, and non-empty dictionary.
🔧 Debug
advanced
2: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
AThe requests.get call is missing required headers causing a connection error
BThe external API is too slow and exceeds the 1 second timeout causing TimeoutError
CThe test assertion is incorrect and causes AssertionError
DThe function get_data returns None causing TypeError
Attempts:
2 left
💡 Hint
Consider the timeout parameter in requests.get and the error type.
framework
advanced
1: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?
Amodule - runs setup once per module
Bfunction - runs setup before each test function
Csession - runs setup once per test session
Dclass - runs setup once per test class
Attempts:
2 left
💡 Hint
Think about how often you want to initialize the external service for all tests.
🧠 Conceptual
expert
2: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?
ARun the tests only manually when needed
BIgnore the flaky tests to avoid blocking the pipeline
CIncrease the test timeout indefinitely to wait for the service
DUse test retries with exponential backoff and mock the external service in CI
Attempts:
2 left
💡 Hint
Consider reliability and automation best practices.