{"id":5186,"library":"django-coverage-plugin","title":"Django Coverage Plugin","description":"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.","status":"active","version":"3.2.2","language":"en","source_language":"en","source_url":"https://github.com/coveragepy/django_coverage_plugin","tags":["Django","testing","coverage","templates","plugin","workflow"],"install":[{"cmd":"pip install django-coverage-plugin","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for code coverage measurement; requires coverage.py 6.x or higher.","package":"coverage","optional":false},{"reason":"Framework for which the plugin provides template coverage; supports Django 5.2 through 6.0.","package":"Django","optional":false},{"reason":"Runtime environment; requires Python 3.10 through 3.14.","package":"python","optional":false}],"imports":[{"note":"The plugin is activated via configuration in .coveragerc or pyproject.toml, not through direct Python import of a class or function.","symbol":"django_coverage_plugin","correct":"# In .coveragerc\n[run]\nplugins = django_coverage_plugin\n\n# Or in pyproject.toml\n[tool.coverage.run]\nplugins = [\n    'django_coverage_plugin',\n]"}],"quickstart":{"code":"import os\n\n# Assume a Django project structure like:\n# myproject/\n#   manage.py\n#   myproject/\n#     settings.py\n#     ...\n#   myapp/\n#     templates/\n#       myapp/my_template.html\n\n# 1. Ensure Django settings are configured for template debugging\n#    In myproject/settings.py:\n#    TEMPLATES = [\n#        {\n#            'BACKEND': 'django.template.backends.django.DjangoTemplates',\n#            'DIRS': [],\n#            'APP_DIRS': True,\n#            'OPTIONS': {\n#                'context_processors': [\n#                    'django.template.context_processors.debug',\n#                    # ... other context processors\n#                ],\n#                'debug': True, # THIS IS CRUCIAL!\n#            },\n#        },\n#    ]\n\n# 2. Create or modify your .coveragerc or pyproject.toml\n#    For .coveragerc:\n#    [run]\n#    plugins = django_coverage_plugin\n#    source = .\n#\n#    For pyproject.toml:\n#    [tool.coverage.run]\n#    plugins = [\n#        'django_coverage_plugin',\n#    ]\n#    source = ['.']\n\n# 3. Set DJANGO_SETTINGS_MODULE environment variable (if not already set)\n#    This is typically handled by manage.py, but explicit setting can avoid ImproperlyConfigured errors.\nos.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get('DJANGO_SETTINGS_MODULE', 'myproject.settings')\n\n# 4. Run tests with coverage\n#    Execute this command in your project's root directory (where manage.py is):\n#    coverage run manage.py test\n#\n# 5. Generate a report (e.g., HTML)\n#    coverage html\n#    (This will create an 'htmlcov' directory with the report, including template coverage.)\n\nprint(\"Setup complete. Run 'coverage run manage.py test' then 'coverage html' from your project root.\")\nprint(f\"DJANGO_SETTINGS_MODULE is set to: {os.environ['DJANGO_SETTINGS_MODULE']}\")\n\n# Example of running tests with coverage (conceptually):\n# import subprocess\n# try:\n#     subprocess.run(['coverage', 'run', 'manage.py', 'test'], check=True)\n#     subprocess.run(['coverage', 'html'], check=True)\n#     print(\"Coverage report generated in htmlcov/\")\n# except subprocess.CalledProcessError as e:\n#     print(f\"Error running coverage: {e}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python and Django environments to currently supported versions (Python 3.10-3.14, Django 5.2-6.0 for v3.2.2) or use an older plugin version compatible with your environment.","message":"Version 3.2.2 dropped support for Django 3.x and 4.x. Version 3.2.0 dropped support for Python 3.9 and Django 2.2. Version 3.1.1 dropped Python 3.8. Version 3.1.0 dropped Python 3.7 and Django 1.x. Version 3.0.0 dropped Python 2.7, 3.6, and Django 1.8. Ensure your Python and Django versions are compatible with the installed plugin version.","severity":"breaking","affected_versions":"3.0.0, 3.1.0, 3.1.1, 3.2.0, 3.2.2"},{"fix":"Set `TEMPLATES[0]['OPTIONS']['debug'] = True` in your Django settings.py.","message":"Django's template debugging must be enabled (`TEMPLATES.OPTIONS.debug = True`) in your settings file for the plugin to work correctly. If not enabled, template coverage will not be measured, or the plugin may raise a `DjangoTemplatePluginException`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `DJANGO_SETTINGS_MODULE` is set, e.g., `os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'` or by running via `manage.py test` as `coverage run manage.py test`.","message":"The `DJANGO_SETTINGS_MODULE` environment variable must be set correctly, especially when running `coverage run` directly without `manage.py` properly bootstrapping the environment. Failure to set this can lead to `django.core.exceptions.ImproperlyConfigured` errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to version 3.2.2 or newer to resolve this false reporting.","message":"In versions prior to 3.2.2, `{% endblock %}` lines (and similar tags) could be falsely reported as unexecuted if they appeared on their own indented line.","severity":"gotcha","affected_versions":"<3.2.2"},{"fix":"Be aware of this limitation; these files will not appear in your template coverage report.","message":"Files included by Django's `{% ssi %}` tag are not included in the coverage measurements by this plugin.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}