0
0
Flaskframework~20 mins

Test client for request simulation in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Test Client Master
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 test client code?

Consider this Flask app and test client code snippet. What will be the printed output?

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

@app.route('/hello')
def hello():
    return jsonify(message='Hello, Test!')

with app.test_client() as client:
    response = client.get('/hello')
    print(response.json)
Anull
B{'message': 'hello, test!'}
C{'message': 'Hello, Test!'}
DSyntaxError
Attempts:
2 left
💡 Hint

Check what response.json returns when the endpoint returns a JSON response.

state_output
intermediate
2:00remaining
What is the status code after this test client POST request?

Given this Flask app and test client POST request, what is the value of response.status_code?

Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['POST'])
def submit():
    data = request.json
    if data and 'name' in data:
        return 'Received', 201
    return 'Bad Request', 400

with app.test_client() as client:
    response = client.post('/submit', json={'name': 'Alice'})
    print(response.status_code)
A200
B500
C400
D201
Attempts:
2 left
💡 Hint

Look at the return status code when the JSON contains the 'name' key.

🔧 Debug
advanced
2:00remaining
Why does this Flask test client code raise an error?

Examine this Flask test client code. Why does it raise an error?

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/test')
def test():
    return 'OK'

with app.test_client() as client:
    response = client.post('/test')
    print(response.status_code)
A405 Method Not Allowed error because '/test' only allows GET by default
B404 Not Found error because '/test' route does not exist
C500 Internal Server Error due to missing return statement
D200 OK because POST works on '/test' route
Attempts:
2 left
💡 Hint

Check the allowed HTTP methods for the route.

📝 Syntax
advanced
2:00remaining
Which option correctly simulates a PUT request with JSON data using Flask test client?

Choose the correct code snippet to send a PUT request with JSON data {'id': 1} using Flask's test client.

Flask
from flask import Flask
app = Flask(__name__)

with app.test_client() as client:
    # Choose the correct option to send PUT with JSON
    pass
Aclient.put('/update', content_type='application/json', data={'id': 1})
Bclient.put('/update', json={'id': 1})
Cclient.put('/update', data='{'id': 1}')
Dclient.put('/update', data={'id': 1})
Attempts:
2 left
💡 Hint

Use the test client parameter that automatically serializes JSON.

🧠 Conceptual
expert
3:00remaining
What happens to Flask app context during test client requests?

When using Flask's app.test_client() to simulate requests, which statement about the application context is true?

AEach test client request automatically pushes and pops the app context, so you can access <code>flask.g</code> and <code>current_app</code> inside the request.
BThe app context is never pushed during test client requests; you must manually push it to access <code>flask.g</code>.
CThe test client disables app context to speed up tests, so <code>flask.g</code> is always null.
DThe app context is shared globally and never popped, so test client requests can cause context leaks.
Attempts:
2 left
💡 Hint

Think about how Flask manages context during simulated requests.