0
0
Djangoframework~5 mins

Coverage reporting in Django

Choose your learning style9 modes available
Introduction

Coverage reporting helps you see which parts of your Django code are tested. It shows what is tested and what is not, so you can improve your tests.

You want to check if your Django views have enough tests.
You want to find untested parts of your Django models or forms.
You want to improve your test quality before releasing your app.
You want to measure how much of your Django project your tests cover.
Syntax
Django
coverage run manage.py test
coverage report
coverage html

coverage run manage.py test runs your tests and collects coverage data.

coverage report shows a summary in the terminal.

coverage html creates a detailed report you can open in a browser.

Examples
Run tests only for the app named myapp and collect coverage data.
Django
coverage run manage.py test myapp
Show coverage report with missing lines marked.
Django
coverage report -m
Generate an HTML report you can open in a browser to see coverage details.
Django
coverage html
Sample Program

This example shows the steps to run coverage on your Django tests. First, install the coverage tool. Then run your tests with coverage enabled. Next, see a summary report in the terminal. Finally, create a detailed HTML report.

Django
1. Install coverage: pip install coverage
2. Run tests with coverage: coverage run manage.py test
3. Show coverage report: coverage report -m
4. Generate HTML report: coverage html
OutputSuccess
Important Notes

Make sure to install the coverage package before running coverage commands.

Run coverage commands from your Django project root where manage.py is located.

Use the HTML report to visually explore which lines are missing tests.

Summary

Coverage reporting shows which parts of your Django code are tested.

Use coverage run manage.py test to collect coverage data.

View results with coverage report or coverage html for detailed info.