0
0
Flaskframework~30 mins

Testing routes and responses in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Testing routes and responses
📖 Scenario: You are building a simple web app using Flask. You want to make sure your routes return the correct responses.
🎯 Goal: Create a Flask app with one route, then write tests to check the route's response status code and content.
📋 What You'll Learn
Create a Flask app with a route /hello that returns 'Hello, World!'
Create a test client to test the Flask app
Write a test to check the status code of the /hello route is 200
Write a test to check the response data of the /hello route contains 'Hello, World!'
💡 Why This Matters
🌍 Real World
Testing routes ensures your web app works correctly before users see it. It helps catch bugs early.
💼 Career
Web developers often write tests for their APIs and web routes to maintain quality and reliability.
Progress0 / 4 steps
1
Create a Flask app with a route
Create a Flask app called app and add a route /hello that returns the string 'Hello, World!'.
Flask
Need a hint?

Use Flask(__name__) to create the app and @app.route('/hello') to define the route.

2
Create a test client
Create a test client from the app by calling app.test_client() and assign it to a variable called client.
Flask
Need a hint?

Use app.test_client() to create the test client.

3
Write a test for status code
Use the client to send a GET request to '/hello' and assign the response to a variable called response. Then check if response.status_code equals 200.
Flask
Need a hint?

Use client.get('/hello') to get the response and check response.status_code.

4
Write a test for response content
Check if the response.data contains the bytes of 'Hello, World!' by asserting b'Hello, World!' in response.data.
Flask
Need a hint?

Remember response data is bytes, so use b'Hello, World!'.