0
0
Flaskframework~30 mins

Coverage reporting in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Coverage Reporting in Flask
📖 Scenario: You are building a small Flask web application and want to check how much of your code is tested by your tests. This helps you find parts of your code that need more testing.
🎯 Goal: Create a Flask app, set up coverage configuration, run tests with coverage, and generate a coverage report.
📋 What You'll Learn
Create a simple Flask app with one route
Add a configuration variable to enable testing mode
Write a test function to test the route
Run coverage to measure test coverage and generate a report
💡 Why This Matters
🌍 Real World
Coverage reporting helps developers see which parts of their web app are tested and which are not, improving code quality.
💼 Career
Understanding coverage reporting is important for software developers and testers to ensure reliable and maintainable code.
Progress0 / 4 steps
1
Create a simple Flask app
Create a Flask app by writing from flask import Flask and then create an app variable with app = Flask(__name__). Add a route / that returns the string 'Hello, Coverage!'.
Flask
Need a hint?

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

2
Add testing configuration
Add a configuration variable app.config['TESTING'] = True to enable testing mode in the Flask app.
Flask
Need a hint?

Set app.config['TESTING'] = True right after creating the app.

3
Write a test function for the route
Write a test function called test_home that uses the Flask test client to send a GET request to '/' and asserts the response data equals b'Hello, Coverage!'.
Flask
Need a hint?

Use app.test_client() to create a client and send a GET request to '/'.

4
Run coverage and generate report
Add the code to run coverage by importing coverage, starting coverage with cov = coverage.Coverage() and cov.start(), then run the test function test_home(), stop coverage with cov.stop(), save coverage data with cov.save(), and finally generate a report with cov.report().
Flask
Need a hint?

Use the coverage module to measure coverage around the test function call.