Django Coverage Plugin

3.2.2 · active · verified Mon Apr 13

django-coverage-plugin is a coverage.py plugin designed to measure test coverage of Django templates. It allows developers to see how much of their template code is exercised by their test suite, integrating template coverage reports alongside Python module coverage. The current version is 3.2.2 and it is actively maintained with regular releases to support new Python and Django versions.

Warnings

Install

Imports

Quickstart

To quickly get started, install the plugin, configure your `coverage.py` settings (either in `.coveragerc` or `pyproject.toml`) to include `django_coverage_plugin` in the `plugins` list, and ensure Django's template debugging is enabled (`TEMPLATES.OPTIONS.debug: True`). You might also need to set the `DJANGO_SETTINGS_MODULE` environment variable. Then, run your Django tests using the `coverage run` command, followed by `coverage html` to generate an interactive HTML report that includes template coverage.

import os

# Assume a Django project structure like:
# myproject/
#   manage.py
#   myproject/
#     settings.py
#     ...
#   myapp/
#     templates/
#       myapp/my_template.html

# 1. Ensure Django settings are configured for template debugging
#    In myproject/settings.py:
#    TEMPLATES = [
#        {
#            'BACKEND': 'django.template.backends.django.DjangoTemplates',
#            'DIRS': [],
#            'APP_DIRS': True,
#            'OPTIONS': {
#                'context_processors': [
#                    'django.template.context_processors.debug',
#                    # ... other context processors
#                ],
#                'debug': True, # THIS IS CRUCIAL!
#            },
#        },
#    ]

# 2. Create or modify your .coveragerc or pyproject.toml
#    For .coveragerc:
#    [run]
#    plugins = django_coverage_plugin
#    source = .
#
#    For pyproject.toml:
#    [tool.coverage.run]
#    plugins = [
#        'django_coverage_plugin',
#    ]
#    source = ['.']

# 3. Set DJANGO_SETTINGS_MODULE environment variable (if not already set)
#    This is typically handled by manage.py, but explicit setting can avoid ImproperlyConfigured errors.
os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get('DJANGO_SETTINGS_MODULE', 'myproject.settings')

# 4. Run tests with coverage
#    Execute this command in your project's root directory (where manage.py is):
#    coverage run manage.py test
#
# 5. Generate a report (e.g., HTML)
#    coverage html
#    (This will create an 'htmlcov' directory with the report, including template coverage.)

print("Setup complete. Run 'coverage run manage.py test' then 'coverage html' from your project root.")
print(f"DJANGO_SETTINGS_MODULE is set to: {os.environ['DJANGO_SETTINGS_MODULE']}")

# Example of running tests with coverage (conceptually):
# import subprocess
# try:
#     subprocess.run(['coverage', 'run', 'manage.py', 'test'], check=True)
#     subprocess.run(['coverage', 'html'], check=True)
#     print("Coverage report generated in htmlcov/")
# except subprocess.CalledProcessError as e:
#     print(f"Error running coverage: {e}")

view raw JSON →