0
0
Flaskframework~10 mins

Coverage reporting in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Coverage reporting
Write Flask app code
Write test functions
Run tests with coverage tool
Coverage tool tracks executed lines
Generate coverage report
Review report to find untested code
Improve tests and repeat
This flow shows how coverage reporting tracks which parts of your Flask app code run during tests, helping you find untested code.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, world!'

# Run tests with coverage
A simple Flask app with one route; coverage tracks which lines run when tests call this route.
Execution Table
StepActionCode LineCoverage StatusNotes
1Start coverage toolN/ANo lines executed yetCoverage tool initialized
2Run test calling '/' routedef home():ExecutedFunction home is called
3Return responsereturn 'Hello, world!'ExecutedReturn statement runs
4Test endsN/ANo new lines executedTest finished
5Generate coverage reportAll lineshome() lines: covered; others: not coveredReport shows tested and untested lines
💡 Tests complete; coverage report generated showing which lines ran
Variable Tracker
VariableStartAfter Step 2After Step 3Final
coverage_statusNot startedTracking startedLines in home() marked executedReport generated with coverage info
Key Moments - 2 Insights
Why does coverage report show some lines as not executed even though the app runs?
Because coverage only tracks lines run during tests; if tests don't call certain routes or functions, those lines remain unexecuted as shown in the execution_table step 5.
Does coverage track code outside of test runs?
No, coverage only tracks code executed during the test run session, as seen in execution_table steps 2-4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which step marks the function 'home' as executed?
AStep 2
BStep 1
CStep 5
DStep 4
💡 Hint
Check the 'Coverage Status' column in step 2 where 'def home()' is marked executed.
At which step does the coverage tool generate the report?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for the step mentioning 'Generate coverage report' in the 'Action' column.
If a test does not call the '/' route, how would the coverage status change in the report?
ALines in home() would be marked as executed
BCoverage tool would fail to run
CLines in home() would be marked as not executed
DAll lines would be marked executed
💡 Hint
Refer to the 'Coverage Status' in step 5 showing lines not executed if tests skip them.
Concept Snapshot
Coverage reporting in Flask:
- Write your Flask app and tests.
- Run tests with coverage tool (e.g., coverage.py).
- Coverage tracks which lines run during tests.
- Generate report to see tested vs untested code.
- Use report to improve test coverage.
Full Transcript
Coverage reporting in Flask helps you see which parts of your app code run during tests. You write your Flask app and test functions. When you run tests with a coverage tool, it tracks each line executed. After tests finish, the tool generates a report showing which lines were tested and which were not. This helps you find untested code so you can add more tests. The process starts with writing code, running tests with coverage, tracking executed lines, generating the report, and then improving tests based on the report.