0
0
Djangoframework~10 mins

Coverage reporting in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Coverage reporting
Write Django Tests
Run Tests with Coverage
Coverage Tool Tracks Lines
Generate Coverage Report
Review Report for Missing Coverage
Improve Tests and Repeat
This flow shows how coverage reporting works in Django: write tests, run them with coverage tracking, generate a report, and improve tests based on it.
Execution Sample
Django
coverage run manage.py test
coverage report
coverage html
Run Django tests with coverage tracking, then show a text report and generate an HTML report.
Execution Table
StepActionCoverage Tool BehaviorOutput
1Run 'coverage run manage.py test'Starts coverage tracking, runs all Django testsTests run, coverage data collected
2Tests execute lines in views.pyCoverage tool marks executed lines as coveredCoverage data updated for views.py
3Tests execute lines in models.pyCoverage tool marks executed lines as coveredCoverage data updated for models.py
4Tests skip some lines in utils.pyCoverage tool marks those lines as missing coverageCoverage data shows missing lines in utils.py
5Run 'coverage report'Summarizes coverage data in terminalText report shows % covered per file
6Run 'coverage html'Generates detailed HTML report with line highlightsHTML files created in 'htmlcov' folder
7Open 'htmlcov/index.html'View detailed coverage with green/red linesVisual report shows covered and missed lines
8Review report and add tests for missed linesCoverage data will improve on next runCycle repeats for better coverage
💡 Coverage report generated and reviewed; testing cycle can repeat to improve coverage.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
coverage_dataemptyviews.py lines coveredmodels.py lines coveredutils.py missing lines notedcomplete coverage data with misses
Key Moments - 2 Insights
Why do some lines show as missing coverage even though tests run?
Because those lines were not executed by any test, as shown in execution_table row 4 where utils.py lines are skipped.
What does the 'coverage html' command do differently than 'coverage report'?
'coverage html' creates a detailed visual report with colored lines, while 'coverage report' shows a summary in the terminal, as seen in rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 4?
AAll lines are covered by tests
BCoverage tool marks some lines as missing coverage
CTests fail to run
DHTML report is generated
💡 Hint
Check the 'Coverage Tool Behavior' column at step 4 in the execution_table.
At which step is the HTML coverage report created?
AStep 2
BStep 3
CStep 6
DStep 8
💡 Hint
Look for 'Generates detailed HTML report' in the execution_table.
If you add tests for missed lines, what will happen to coverage_data after rerunning tests?
ACoverage data will show more lines covered
BCoverage data will be empty
CCoverage data will show fewer lines covered
DCoverage data will not change
💡 Hint
Refer to the last row in execution_table about improving coverage by adding tests.
Concept Snapshot
Coverage Reporting in Django:
- Run tests with 'coverage run manage.py test'
- Use 'coverage report' for terminal summary
- Use 'coverage html' for detailed visual report
- Review missing lines and add tests
- Repeat to improve coverage percentage
Full Transcript
Coverage reporting in Django helps you see which parts of your code are tested. You run your tests with coverage tracking using 'coverage run manage.py test'. The coverage tool watches which lines run during tests. Then you generate a report with 'coverage report' to see a summary in the terminal or 'coverage html' to get a detailed colored report in your browser. This report shows green lines for covered code and red lines for missed code. You can then add tests for missed lines and run coverage again to improve your test coverage. This cycle helps you write better tested Django apps.