A. The attribute should be response.status_code, not response.status.
B. The test client must be created with FlaskClient() instead.
C. The assert statement should check response.data, not status.
D. The get() method requires a POST request instead.
Solution
Step 1: Identify correct attribute for HTTP status
Flask response objects use status_code to hold the HTTP status number, not status.
Step 2: Check the assert statement
The assert should compare response.status_code == 200 to be valid.
Final Answer:
The attribute should be response.status_code, not response.status. -> Option A
Quick Check:
Use response.status_code for HTTP status [OK]
Hint: Use response.status_code, not response.status [OK]
Common Mistakes:
Using response.status instead of response.status_code
Thinking get() requires POST method
Confusing assert target with response data
5. You want to write a test to check that submitting a form to '/submit' returns a redirect (status 302). Which test code snippet correctly checks this?
hard
A. response = client.post('/submit')
assert response.data == 'Redirected'
B. response = client.get('/submit')
assert response.status_code == 200
C. response = client.post('/submit', data={'name': 'Alice'})
assert response.status_code == 302
D. response = client.post('/submit', data={'name': 'Alice'})
assert response.status == 200
Solution
Step 1: Identify correct HTTP method and data submission
To submit a form, use POST with data dictionary as in response = client.post('/submit', data={'name': 'Alice'})
assert response.status_code == 302.
Step 2: Check correct status code for redirect
A redirect returns status code 302, so assert should check response.status_code == 302.
Step 3: Validate options
response = client.post('/submit', data={'name': 'Alice'})
assert response.status_code == 302 correctly posts data and asserts status 302. response = client.post('/submit', data={'name': 'Alice'})
assert response.status == 200 incorrectly asserts status 200. The other options use wrong methods or checks.