0
0
Djangoframework~30 mins

Coverage reporting in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Coverage Reporting in Django
📖 Scenario: You are working on a Django project and want to check how much of your code is tested by your tests. This helps you find parts of your code that need more tests.
🎯 Goal: Set up coverage reporting in your Django project to measure test coverage and generate a report.
📋 What You'll Learn
Create a Django test file with a simple test case
Add a coverage configuration variable
Run tests with coverage measurement
Generate a coverage report in the terminal
💡 Why This Matters
🌍 Real World
Coverage reporting helps developers see which parts of their Django code are tested and which are not. This improves code quality and confidence.
💼 Career
Many software development jobs require writing tests and measuring coverage to ensure reliable applications.
Progress0 / 4 steps
1
Create a simple Django test case
Create a file called tests.py in your Django app folder. Inside it, write a test class called SimpleTest that inherits from django.test.TestCase. Add a test method called test_addition that checks if 1 + 1 equals 2 using self.assertEqual.
Django
Need a hint?

Remember to import TestCase from django.test. Your test method name must start with test_.

2
Add a coverage configuration variable
Create a variable called COVERAGE_CONFIG and set it to the string ".coveragerc". This will be the name of the coverage configuration file.
Django
Need a hint?

This variable holds the filename for coverage settings.

3
Run tests with coverage measurement
Write a command string called coverage_command that runs coverage to measure tests. Set coverage_command to "coverage run --rcfile=.coveragerc manage.py test".
Django
Need a hint?

This command runs tests with coverage using the config file.

4
Generate a coverage report
Write a command string called report_command that generates a coverage report in the terminal. Set report_command to "coverage report --rcfile=.coveragerc".
Django
Need a hint?

This command shows the coverage summary in the terminal.