0
0
Flaskframework~20 mins

Testing routes and responses in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the response status code for a successful GET request?
Consider a Flask route that returns a simple message with status code 200. What status code will the test client receive when making a GET request to this route?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello, world!', 200

with app.test_client() as client:
    response = client.get('/hello')
    status_code = response.status_code
A302
B404
C500
D200
Attempts:
2 left
💡 Hint
Think about the HTTP status code for a successful request.
📝 Syntax
intermediate
2:00remaining
Which option correctly tests a POST route with JSON data?
You want to test a Flask POST route that accepts JSON data. Which test client call is correct?
Flask
from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/data', methods=['POST'])
def data():
    json_data = request.get_json()
    return jsonify(received=json_data), 201

with app.test_client() as client:
    response = client.post('/data', json={'key': 'value'})
    status_code = response.status_code
Aclient.post('/data', json={'key': 'value'})
Bclient.post('/data', data='key=value')
Cclient.post('/data', content_type='application/json')
Dclient.post('/data', data={'key': 'value'})
Attempts:
2 left
💡 Hint
Use the test client method that automatically sets JSON headers and serializes data.
🔧 Debug
advanced
2:00remaining
Why does this test fail with a 405 Method Not Allowed error?
Given this Flask route and test, why does the test fail with a 405 error?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/item', methods=['GET'])
def get_item():
    return 'Item details'

with app.test_client() as client:
    response = client.post('/item')
    status_code = response.status_code
AThe route is missing a POST method handler.
BThe test client is not properly initialized.
CThe route only allows GET requests, but the test uses POST.
DThe route URL is incorrect.
Attempts:
2 left
💡 Hint
Check the allowed HTTP methods for the route.
state_output
advanced
2:00remaining
What is the value of response.json after this test?
A Flask route returns JSON data. What will the test client receive in response.json?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/info')
def info():
    return jsonify(name='Alice', age=30)

with app.test_client() as client:
    response = client.get('/info')
    data = response.json
Anull
B{'name': 'Alice', 'age': 30}
C{'name': 'Alice'}
D{'name': 'Alice', 'age': '30'}
Attempts:
2 left
💡 Hint
Check the JSON keys and value types returned by the route.
🧠 Conceptual
expert
2:30remaining
Which statement about Flask test client context is true?
When using Flask's test client in a with-statement, which is correct about the application context?
AThe application context is automatically pushed and popped inside the with-statement.
BYou must manually push the application context before using the test client.
CThe test client does not manage any context; you must handle it yourself.
DThe test client disables the application context during tests.
Attempts:
2 left
💡 Hint
Think about how Flask manages contexts during testing.