0
0
Flaskframework~20 mins

Mocking external services in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route when the external API is mocked?

Consider this Flask route that calls an external API to get user data. The external API is mocked to return a fixed response.

import requests
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/user/')
def get_user(user_id):
    response = requests.get(f'https://api.example.com/users/{user_id}')
    data = response.json()
    return jsonify({'name': data['name'], 'id': data['id']})

# Mock setup:
# requests.get is mocked to always return a response with json() = {'id': 42, 'name': 'Alice'}

What will be the JSON output when accessing /user/10?

A{"name": "Unknown", "id": 10}
B{"name": "Alice", "id": 42}
C{"name": "Alice", "id": 10}
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Remember the mock always returns the same data regardless of the user_id.

🔧 Debug
intermediate
2:00remaining
Why does this Flask test fail when mocking an external service?

In this Flask test, the external API call is mocked but the test fails with a TypeError.

import pytest
from unittest.mock import patch

@patch('requests.get')
def test_user(mock_get, client):
    mock_get.return_value.json = {'id': 1, 'name': 'Bob'}
    response = client.get('/user/1')
    assert response.json == {'id': 1, 'name': 'Bob'}

What is the cause of the error?

Amock_get.return_value.json should be a callable, not a dict
BThe patch decorator is applied to the wrong function
CThe Flask client does not support .json attribute on response
DThe test is missing a Flask app context
Attempts:
2 left
💡 Hint

Check what response.json expects to be in the mock.

📝 Syntax
advanced
2:00remaining
Which option correctly mocks a Flask external API call using unittest.mock?

You want to mock requests.get in a Flask test to return a JSON response {'status': 'ok'}. Which code snippet is correct?

A
with patch('requests.get') as mock_get:
    mock_get.return_value.json = lambda: {'status': 'ok'}
B
with patch('requests.get') as mock_get:
    mock_get.return_value.json = {'status': 'ok'}
C
with patch('requests.get') as mock_get:
    mock_get.return_value = {'status': 'ok'}
D
with patch('requests.get') as mock_get:
    mock_get.json.return_value = {'status': 'ok'}
Attempts:
2 left
💡 Hint

The mocked json must be a callable method returning the dict.

state_output
advanced
2:00remaining
What is the value of variable 'result' after mocking an external API in Flask?

Given this Flask function and test snippet:

def fetch_data():
    import requests
    response = requests.get('https://api.example.com/data')
    return response.json()['value']

# Test with mock:
from unittest.mock import patch

with patch('requests.get') as mock_get:
    mock_get.return_value.json = lambda: {'value': 123}
    result = fetch_data()

What is the value of result?

ARaises a TypeError
B{'value': 123}
CNone
D123
Attempts:
2 left
💡 Hint

Look at what fetch_data returns from the mocked JSON.

🧠 Conceptual
expert
2:00remaining
Which option best explains why mocking external services is important in Flask testing?

Why do developers mock external API calls when testing Flask applications?

ATo avoid writing any test code and rely on production data
BTo increase the complexity of tests by adding real API calls
CTo isolate tests from network issues and ensure consistent, fast, and reliable test results
DTo make tests slower and dependent on external service uptime
Attempts:
2 left
💡 Hint

Think about test speed and reliability.