Bird
Raised Fist0
Flaskframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using app.test_client() in Flask testing?
easy
A. To create a database connection for testing
B. To start the Flask server for manual testing
C. To simulate HTTP requests to routes without running the server
D. To generate HTML templates automatically

Solution

  1. Step 1: Understand the role of app.test_client()

    This method creates a client that can send HTTP requests to the Flask app internally without needing the server to run.
  2. Step 2: Compare with other options

    Starting the server or creating a database connection are unrelated to test_client(). It does not generate templates either.
  3. Final Answer:

    To simulate HTTP requests to routes without running the server -> Option C
  4. Quick Check:

    app.test_client() simulates requests [OK]
Hint: Remember test_client() simulates requests internally [OK]
Common Mistakes:
  • Thinking test_client() starts the server
  • Confusing test_client() with database setup
  • Assuming it generates HTML templates
2. Which of the following is the correct way to test a GET request to the '/' route using Flask's test client?
easy
A. response = client.post('/')
B. response = client.get('/')
C. response = client.request('PUT', '/')
D. response = client.fetch('/')

Solution

  1. Step 1: Identify the HTTP method for the route

    The question asks for a GET request, so the method should be get().
  2. Step 2: Match the method with the test client syntax

    Flask test client uses client.get(path) to send GET requests. Other methods like post() or request('PUT', ...) are for different HTTP verbs.
  3. Final Answer:

    response = client.get('/') -> Option B
  4. Quick Check:

    GET request uses client.get() [OK]
Hint: Use client.get() for GET requests in tests [OK]
Common Mistakes:
  • Using post() instead of get() for GET requests
  • Using client.fetch() which does not exist
  • Confusing HTTP methods in test calls
3. Given this Flask test code:
with app.test_client() as client:
    response = client.get('/hello')
    print(response.status_code)
    print(response.data.decode())

If the '/hello' route returns 'Hello, World!' with status 200, what will be printed?
medium
A. 200 Hello, World!
B. 404 Not Found
C. 500 Internal Server Error
D. 200 b'Hello, World!'

Solution

  1. Step 1: Understand the route response

    The route '/hello' returns the string 'Hello, World!' with HTTP status 200.
  2. Step 2: Analyze the test output

    The response.status_code will be 200. The response.data is bytes, so decoding it with decode() converts it to the string 'Hello, World!'.
  3. Final Answer:

    200 Hello, World! -> Option A
  4. Quick Check:

    Status 200 and decoded response text [OK]
Hint: Decode response.data to get string content [OK]
Common Mistakes:
  • Not decoding response.data (shows bytes)
  • Expecting 404 or 500 status codes incorrectly
  • Confusing status_code with response content
4. What is wrong with this Flask test code snippet?
client = app.test_client()
response = client.get('/missing')
assert response.status_code == 200

Assuming '/missing' route does not exist.
medium
A. The status code will be 404, so the assertion fails
B. The get method should be post for missing routes
C. The test client must be used inside a context manager
D. The response object does not have status_code attribute

Solution

  1. Step 1: Check the route existence

    The route '/missing' does not exist, so Flask returns a 404 Not Found status.
  2. Step 2: Evaluate the assertion

    The assertion expects status code 200, but the actual is 404, causing the test to fail.
  3. Final Answer:

    The status code will be 404, so the assertion fails -> Option A
  4. Quick Check:

    Missing route returns 404, assertion expects 200 [OK]
Hint: Check route exists before asserting status 200 [OK]
Common Mistakes:
  • Assuming missing routes return 200
  • Thinking get() must be post() for missing routes
  • Believing status_code attribute is missing
5. You want to test a Flask route that returns JSON data {"success": true, "count": 5} with status 201. Which test code correctly verifies both the status and JSON content?
hard
A. response = client.get('/data') assert response.status_code == 201 assert response.json() == {"success": True, "count": 5}
B. response = client.get('/data') assert response.status_code == 200 assert response.json == {"success": True, "count": 5}
C. response = client.post('/data') assert response.status_code == 200 assert response.get_json() == {"success": True, "count": 5}
D. response = client.get('/data') assert response.status_code == 201 assert response.get_json() == {"success": True, "count": 5}

Solution

  1. Step 1: Check HTTP method and status code

    The route returns status 201, so the test must assert response.status_code == 201. The method is GET as per the question.
  2. Step 2: Verify JSON content correctly

    Flask test response uses get_json() to parse JSON data. Using response.json is incorrect in Flask.
  3. Final Answer:

    response = client.get('/data') assert response.status_code == 201 assert response.get_json() == {"success": True, "count": 5} -> Option D
  4. Quick Check:

    Use get_json() and check status 201 [OK]
Hint: Use get_json() to check JSON response content [OK]
Common Mistakes:
  • Using response.json instead of get_json()
  • Checking wrong status code (200 instead of 201)
  • Using POST instead of GET method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard