0
0
Flaskframework~10 mins

Mocking external services in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask testing client.

Flask
from flask import Flask
from flask.testing import [1]

app = Flask(__name__)
Drag options to blanks, or click blank then click option'
ATestClient
BClient
CTestRequest
DFlaskClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'TestClient' which is not the correct Flask test client class.
Using 'Client' which is too generic and not from flask.testing.
2fill in blank
medium

Complete the code to patch the external API call using unittest.mock.

Flask
from unittest.mock import patch

@patch('[1]')
def test_api_call(mock_get):
    pass
Drag options to blanks, or click blank then click option'
Arequests.get
Bapp.get
Cflask.get
Dexternal.get
Attempts:
3 left
💡 Hint
Common Mistakes
Patching 'flask.get' which does not exist.
Patching 'app.get' which is unrelated to HTTP calls.
3fill in blank
hard

Fix the error in the mock setup to return a JSON response.

Flask
mock_get.return_value.[1] = lambda: {'status': 'ok'}
Drag options to blanks, or click blank then click option'
Ajson_data
Bjson
Cjson.return_value()
Djson.return_value
Attempts:
3 left
💡 Hint
Common Mistakes
Mocking 'json' as a dict instead of a callable method.
Using 'json.return_value' without calling it as a function.
4fill in blank
hard

Fill both blanks to create a mock response with status code 200 and JSON data.

Flask
mock_response = MagicMock()
mock_response.[1] = 200
mock_response.[2].return_value = {'message': 'success'}
Drag options to blanks, or click blank then click option'
Astatus_code
Bjson
Ccontent
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'content' or 'text' for JSON data instead of mocking 'json' method.
Setting 'status' instead of 'status_code'.
5fill in blank
hard

Fill all three blanks to patch 'requests.get', create a mock response, and assert the call was made.

Flask
with patch('[1]') as mock_get:
    mock_get.return_value.[2] = 200
    response = requests.get('http://example.com')
    mock_get.[3]('http://example.com')
Drag options to blanks, or click blank then click option'
Arequests.get
Bstatus_code
Cassert_called_with
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assert_called_once' instead of 'assert_called_with' which checks arguments.
Setting 'status' instead of 'status_code'.