0
0
Flaskframework~10 mins

Testing routes and responses in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a test client for the Flask app.

Flask
with app.[1]() as client:
    response = client.get('/')
Drag options to blanks, or click blank then click option'
Arun
Btest_request_context
Ctest_client
Dapp_context
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.run() instead of app.test_client()
Using app.app_context() which is for context, not client
Using test_request_context() which is for context, not client
2fill in blank
medium

Complete the code to check the HTTP status code of the response.

Flask
assert response.[1] == 200
Drag options to blanks, or click blank then click option'
Aheaders
Bstatus_code
Ccontent
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Checking response.data instead of status_code
Using response.content which is not a Flask attribute
Checking response.headers instead of status_code
3fill in blank
hard

Fix the error in the test by completing the code to decode the response data.

Flask
assert response.data.[1]('utf-8') == 'Hello, World!'
Drag options to blanks, or click blank then click option'
Adecode
Bsplit
Cencode
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode() which converts string to bytes
Using split() or strip() which do not convert bytes to string
4fill in blank
hard

Fill both blanks to test a POST request with JSON data and check the response JSON.

Flask
response = client.post('/submit', json=[1])
assert response.[2]['status'] == 'success'
Drag options to blanks, or click blank then click option'
A{'name': 'Alice'}
Bjson
Cdata
D{'status': 'success'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing JSON as a string instead of a dictionary
Using response.data instead of response.json for JSON response
5fill in blank
hard

Fill all three blanks to test a route with query parameters and check the response text.

Flask
response = client.get('/search?query=[1]')
assert response.[2] == 200
assert response.data.[3]('utf-8') == 'Results for test'
Drag options to blanks, or click blank then click option'
Atest
Bstatus_code
Cdecode
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Not decoding response data before comparing
Checking response.data instead of response.status_code for status
Using wrong query parameter value