Bird
0
0

What is wrong with this Flask test code snippet?

medium📝 Debug Q14 of 15
Flask - Testing Flask Applications
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.
AThe status code will be 404, so the assertion fails
BThe get method should be post for missing routes
CThe test client must be used inside a context manager
DThe response object does not have status_code attribute
Step-by-Step Solution
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]
Quick Trick: Check route exists before asserting status 200 [OK]
Common Mistakes:
MISTAKES
  • Assuming missing routes return 200
  • Thinking get() must be post() for missing routes
  • Believing status_code attribute is missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes