Coverage reporting helps you see which parts of your Flask app's code are tested. It shows what is covered by tests and what is not.
Coverage reporting in Flask
coverage run -m pytest coverage report coverage html
coverage run -m pytest runs tests while tracking coverage.
coverage report shows coverage summary in the terminal.
coverage html creates a detailed HTML report you can open in a browser.
coverage run -m pytest
coverage report
coverage html
This Flask app has two routes: '/' and '/about'. The test only checks the '/' route. Running coverage will show that '/about' is not tested.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, world!' @app.route('/about') def about(): return 'About page' # test_app.py import pytest from app import app @pytest.fixture def client(): with app.test_client() as client: yield client def test_home(client): response = client.get('/') assert response.data == b'Hello, world!' # Run coverage commands in terminal: # coverage run -m pytest # coverage report # coverage html
Make sure to install coverage with pip install coverage.
Run coverage commands from your project root where your Flask app and tests are.
Use the HTML report to easily see which lines are missing tests.
Coverage reporting shows which parts of your Flask app are tested.
Use coverage run -m pytest to run tests with coverage.
Check results with coverage report or coverage html.
Practice
Solution
Step 1: Understand coverage reporting
Coverage reporting tracks which parts of your code are executed during tests.Step 2: Identify its purpose in Flask
It helps you see which parts of your Flask app have tests and which do not.Final Answer:
To show which parts of the app are tested by your tests -> Option CQuick Check:
Coverage reporting = shows tested parts [OK]
- Confusing coverage with app speed optimization
- Thinking coverage deploys the app
- Assuming coverage creates UI
Solution
Step 1: Recall the coverage run syntax
The correct command to run tests with coverage is 'coverage run -m pytest'.Step 2: Check other options
Other options mix commands incorrectly or use invalid syntax.Final Answer:
coverage run -m pytest -> Option BQuick Check:
Correct coverage test run command = coverage run -m pytest [OK]
- Swapping order of coverage and pytest
- Using 'flask' command with coverage incorrectly
- Typing 'coverage test run' which is invalid
coverage run -m pytest successfully, which command shows a text summary of coverage results?Solution
Step 1: Identify commands for coverage output
'coverage report' shows a text summary in the terminal, while 'coverage html' creates a web page report.Step 2: Eliminate incorrect options
'pytest --coverage' is not a standard command, and 'flask coverage report' is invalid.Final Answer:
coverage report -> Option AQuick Check:
Text summary command = coverage report [OK]
- Confusing 'coverage html' with text output
- Using invalid commands like 'pytest --coverage'
- Trying 'flask coverage report' which doesn't exist
coverage run -m pytest but coverage report shows 0% coverage. What is the likely cause?Solution
Step 1: Understand 0% coverage meaning
0% coverage means no code was executed during tests, so coverage tool found no lines run.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.Final Answer:
Tests did not execute any Flask app code -> Option AQuick Check:
0% coverage = no app code run in tests [OK]
- Assuming 0% means Flask is not installed
- Thinking coverage report command typo causes 0%
- Believing running coverage without pytest causes 0%
Solution
Step 1: Run tests with coverage
Use 'coverage run -m pytest' to run tests and collect coverage data.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.Step 3: Verify other options
Other options use invalid commands or wrong paths for report files.Final Answer:
coverage run -m pytest; coverage html; open htmlcov/index.html -> Option DQuick Check:
Run tests, generate HTML, open htmlcov/index.html [OK]
- Using wrong command order or invalid commands
- Opening wrong file path for HTML report
- Confusing 'pytest run coverage' with correct syntax
Ready to prove it?
Pass the test and this pattern moves unknown
