Bird
Raised Fist0
Flaskframework~10 mins

Coverage reporting in Flask - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of coverage reporting in a Flask application?
easy
A. To improve the speed of the Flask app
B. To deploy the Flask app to a server
C. To show which parts of the app are tested by your tests
D. To create a user interface for the app

Solution

  1. Step 1: Understand coverage reporting

    Coverage reporting tracks which parts of your code are executed during tests.
  2. Step 2: Identify its purpose in Flask

    It helps you see which parts of your Flask app have tests and which do not.
  3. Final Answer:

    To show which parts of the app are tested by your tests -> Option C
  4. Quick Check:

    Coverage reporting = shows tested parts [OK]
Hint: Coverage shows tested code parts, not speed or deployment [OK]
Common Mistakes:
  • Confusing coverage with app speed optimization
  • Thinking coverage deploys the app
  • Assuming coverage creates UI
2. Which command correctly runs tests with coverage in a Flask project?
easy
A. pytest coverage run
B. coverage run -m pytest
C. flask coverage run
D. coverage test run

Solution

  1. Step 1: Recall the coverage run syntax

    The correct command to run tests with coverage is 'coverage run -m pytest'.
  2. Step 2: Check other options

    Other options mix commands incorrectly or use invalid syntax.
  3. Final Answer:

    coverage run -m pytest -> Option B
  4. Quick Check:

    Correct coverage test run command = coverage run -m pytest [OK]
Hint: Use 'coverage run -m pytest' to run tests with coverage [OK]
Common Mistakes:
  • Swapping order of coverage and pytest
  • Using 'flask' command with coverage incorrectly
  • Typing 'coverage test run' which is invalid
3. Given you ran coverage run -m pytest successfully, which command shows a text summary of coverage results?
medium
A. coverage report
B. pytest --coverage
C. coverage html
D. flask coverage report

Solution

  1. Step 1: Identify commands for coverage output

    'coverage report' shows a text summary in the terminal, while 'coverage html' creates a web page report.
  2. Step 2: Eliminate incorrect options

    'pytest --coverage' is not a standard command, and 'flask coverage report' is invalid.
  3. Final Answer:

    coverage report -> Option A
  4. Quick Check:

    Text summary command = coverage report [OK]
Hint: Use 'coverage report' for text summary, 'coverage html' for web report [OK]
Common Mistakes:
  • Confusing 'coverage html' with text output
  • Using invalid commands like 'pytest --coverage'
  • Trying 'flask coverage report' which doesn't exist
4. You ran coverage run -m pytest but coverage report shows 0% coverage. What is the likely cause?
medium
A. Tests did not execute any Flask app code
B. You forgot to install Flask
C. Coverage report command is misspelled
D. You ran coverage without pytest

Solution

  1. Step 1: Understand 0% coverage meaning

    0% coverage means no code was executed during tests, so coverage tool found no lines run.
  2. Step 2: Analyze possible causes

    If tests ran but did not call Flask app code, coverage stays zero. Missing Flask or misspelling commands would cause errors, not zero coverage.
  3. Final Answer:

    Tests did not execute any Flask app code -> Option A
  4. Quick Check:

    0% coverage = no app code run in tests [OK]
Hint: 0% means tests ran but didn't touch app code [OK]
Common Mistakes:
  • Assuming 0% means Flask is not installed
  • Thinking coverage report command typo causes 0%
  • Believing running coverage without pytest causes 0%
5. You want to generate a detailed HTML coverage report for your Flask app after running tests. Which sequence of commands is correct?
hard
A. flask coverage run; coverage html; open html/index.html
B. pytest run coverage; coverage report; open index.html
C. coverage test run; coverage html; open coverage/index.html
D. coverage run -m pytest; coverage html; open htmlcov/index.html

Solution

  1. Step 1: Run tests with coverage

    Use 'coverage run -m pytest' to run tests and collect coverage data.
  2. Step 2: Generate HTML report and open it

    Run 'coverage html' to create the report in 'htmlcov' folder, then open 'htmlcov/index.html' to view it.
  3. Step 3: Verify other options

    Other options use invalid commands or wrong paths for report files.
  4. Final Answer:

    coverage run -m pytest; coverage html; open htmlcov/index.html -> Option D
  5. Quick Check:

    Run tests, generate HTML, open htmlcov/index.html [OK]
Hint: Run tests, then 'coverage html', then open htmlcov/index.html [OK]
Common Mistakes:
  • Using wrong command order or invalid commands
  • Opening wrong file path for HTML report
  • Confusing 'pytest run coverage' with correct syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard