Challenge - 5 Problems
Flask Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1: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
Attempts:
2 left
💡 Hint
Think about the HTTP status code for a successful request.
✗ Incorrect
A status code of 200 means the request was successful and the server returned the expected response.
📝 Syntax
intermediate2: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
Attempts:
2 left
💡 Hint
Use the test client method that automatically sets JSON headers and serializes data.
✗ Incorrect
Using json= in client.post sends JSON data with the correct content type.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check the allowed HTTP methods for the route.
✗ Incorrect
The route only accepts GET requests. Sending a POST request causes a 405 Method Not Allowed error.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
Check the JSON keys and value types returned by the route.
✗ Incorrect
The route returns a JSON object with keys 'name' and 'age' where age is an integer 30.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how Flask manages contexts during testing.
✗ Incorrect
Using the test client as a context manager automatically pushes and pops the application context, simplifying tests.