Bird
0
0

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📝 state output Q15 of 15
Flask - Testing Flask Applications
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?
Aresponse = client.get('/data') assert response.status_code == 201 assert response.json() == {"success": True, "count": 5}
Bresponse = client.get('/data') assert response.status_code == 200 assert response.json == {"success": True, "count": 5}
Cresponse = client.post('/data') assert response.status_code == 200 assert response.get_json() == {"success": True, "count": 5}
Dresponse = client.get('/data') assert response.status_code == 201 assert response.get_json() == {"success": True, "count": 5}
Step-by-Step Solution
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]
Quick Trick: Use get_json() to check JSON response content [OK]
Common Mistakes:
MISTAKES
  • Using response.json instead of get_json()
  • Checking wrong status code (200 instead of 201)
  • Using POST instead of GET method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes