• Stars
    star
    193
  • Rank 194,507 (Top 4 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plugin for coverage.py to measure Django template execution

Django Template Coverage.py Plugin

A coverage.py plugin to measure test coverage of Django templates.


Supported on:

  • Python: 3.8 through 3.12.
  • Django: 2.x, 3.x and 4.x.
  • Coverage.py: 6.x or higher.

The plugin is pip installable:

$ python3 -m pip install django_coverage_plugin

To run it, add this setting to your .coveragerc file:

[run]
plugins = django_coverage_plugin

Then run your tests under coverage.py.

You will see your templates listed in your coverage report along with your Python modules. Please use coverage.py v4.4 or greater to allow the plugin to identify untested templates.

If you get a django.core.exceptions.ImproperlyConfigured error, you need to set the DJANGO_SETTINGS_MODULE environment variable.

Template coverage only works if your Django templates have debugging enabled. If you get django_coverage_plugin.plugin.DjangoTemplatePluginException: Template debugging must be enabled in settings, or if no templates get measured, make sure you have TEMPLATES.OPTIONS.debug set to True in your settings file:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'debug': True,
        },
    },
]

Configuration

The Django template plugin uses some existing settings from your .coveragerc file. The source=, include=, and omit= options control what template files are included in the report.

The plugin can find unused template and include them in your results. By default, it will look for files in your templates directory with an extension of .html, .htm, or .txt. You can configure it to look for a different set of extensions if you like:

[run]
plugins = django_coverage_plugin

[django_coverage_plugin]
template_extensions = html, txt, tex, email

If you use pyproject.toml for tool configuration use:

[tool.coverage.run]
plugins = [
    'django_coverage_plugin',
]

[tool.coverage.django_coverage_plugin]
template_extensions = 'html, txt, tex, email'

Caveats

Coverage.py can't tell whether a {% blocktrans %} tag used the singular or plural text, so both are marked as used if the tag is used.

What the? How?

The technique used to measure the coverage is the same that Dmitry Trofimov used in dtcov, but integrated into coverage.py as a plugin, and made more performant. I'd love to see how well it works in a real production project. If you want to help me with it, feel free to drop me an email.

The coverage.py plugin mechanism is designed to be generally useful for hooking into the collection and reporting phases of coverage.py, specifically to support non-Python files. If you have non-Python files you'd like to support in coverage.py, let's talk.

Tests

To run the tests:

$ python3 -m pip install -r requirements.txt
$ tox

History

v3.1.0 — 2023-07-10

Dropped support for Python 3.7 and Django 1.x. Declared support for Python 3.12.

v3.0.0 — 2022-12-06

Dropped support for Python 2.7, Python 3.6, and Django 1.8.

v2.0.4 — 2022-10-31

Declare our support for Python 3.11 and Django 4.1.

v2.0.3 — 2022-05-04

Add support for Django 4.0.

v2.0.2 — 2021-11-11

If a non-UTF8 file was found when looking for templates, it would fail when reading during the reporting phase, ending execution. This failure is now raised in a way that can be ignored with a .coveragerc setting of [report] ignore_errors=True (issue 78).

When using source=., an existing coverage HTML report directory would be found and believed to be unmeasured HTML template files. This is now fixed.

v2.0.1 — 2021-10-06

Test and claim our support on Python 3.10.

v2.0.0 — 2021-06-08

Drop support for Python 3.4 and 3.5.

A setting is available: template_extensions lets you set the file extensions that will be considered when looking for unused templates (requested in issue 60).

Fix an issue on Windows where file names were being compared case-sensitively, causing templates to be missed (issue 46).

Fix an issue (issue 63) where tag libraries can't be found if imported during test collection. Thanks to Daniel Izquierdo for the fix.

v1.8.0 — 2020-01-23

Add support for:

  • Coverage 5

v1.7.0 — 2020-01-16

Add support for:

  • Python 3.7 & 3.8
  • Django 2.2 & 3.0

v1.6.0 — 2018-09-04

Add support for Django 2.1.

v1.5.2 — 2017-10-18

Validates support for Django version 2.0b1. Improves discovery of template files.

v1.5.1a — 2017-04-05

Validates support for Django version 1.11. Testing for new package maintainer Pamela McA'Nulty

v1.5.0 — 2017-02-23

Removes support for Django versions below 1.8. Validates support for Django version 1.11b1

v1.4.2 — 2017-02-06

Fixes another instance of issue 32, which was the result of an initialization order problem.

v1.4.1 — 2017-01-25

Fixes issue 32, which was the result of an initialization order problem.

v1.4 — 2017-01-16

Django 1.10.5 is now supported.

Checking settings configuration is deferred so that settings.py is included in coverage reporting. Fixes issue 28.

Only the django.template.backends.django.DjangoTemplates template engine is supported, and it must be configured with ['OPTIONS']['debug'] = True. Fixes issue 27.

v1.3.1 — 2016-06-02

Settings are read slightly differently, so as to not interfere with programs that don't need settings. Fixes issue 18.

v1.3 — 2016-04-03

Multiple template engines are allowed. Thanks, Simon Charette.

v1.2.2 — 2016-02-01

No change in code, but Django 1.9.2 is now supported.

v1.2.1 — 2016-01-28

The template debug settings are checked properly for people still using TEMPLATE_DEBUG in newer versions of Django.

v1.2 — 2016-01-16

Check if template debugging is enabled in the settings, and raise a visible warning if not. This prevents mysterious failures of the plugin, and fixes issue 17.

Potential Django 1.9 support is included, but the patch to Django hasn't been applied yet.

v1.1 — 2015-11-12

Explicitly configure settings if need be to get things to work.

v1.0 — 2015-09-20

First version :)

More Repositories

1

coveragepy

The code coverage tool for Python
Python
2,791
star
2

byterun

A Python implementation of a Python bytecode runner
Python
1,266
star
3

watchgha

Live display of current GitHub action runs
Python
326
star
4

cog

Small bits of Python computation for static files
Python
318
star
5

scriv

Changelog management tool
Python
241
star
6

dinghy

A GitHub activity digest tool
Python
181
star
7

pkgsample

A simple example of how to structure a Python project
Python
82
star
8

truchet

Playing with Truchet tiles
Jupyter Notebook
47
star
9

gefilte

Gefilte Fish GMail filter creator
Python
41
star
10

pydoctor

A diagnostic program to show the Python environment
Python
27
star
11

dot

Personal dotfiles
Shell
25
star
12

pytest-gallery

A sampler of tests showing different ways to construct tests for pytest
Python
24
star
13

aptus

Mandelbrot fractal viewer
Python
20
star
14

pylintdb

Put pylint violations into sqlite
Python
19
star
15

cupid

Python
19
star
16

choosy

A Python teaching tool
Python
19
star
17

zellij

A toy for making geometric art, inspired by Islamic Zellij.
Python
18
star
18

flourish

Harmonograph toy
Python
13
star
19

gpxmapper

Python
12
star
20

adventofcode2017

Python
10
star
21

adventofcode2018

Python
9
star
22

unittest-mixins

Helpful unittest mixin classes
Python
9
star
23

adventofcode2020

Python
8
star
24

unipain

PyCon presentation: Pragmatic Unicode, or, How Do I Stop the Pain?
HTML
8
star
25

adventofcode2019

Python
8
star
26

odds

Odds & Ends
Python
8
star
27

iter

PyCon presentation about iteration
HTML
7
star
28

nedbatcom

nedbatchelder.com
HTML
7
star
29

song-basket

Simple Spotify app to collect songs into a basket playlist.
Python
6
star
30

pyhurry

Python
6
star
31

adventofcode2021

Python
6
star
32

nedbat

nedbat's profile
Shell
5
star
33

adventofcode2015

Python
5
star
34

adventofcode2022

adventofcode 2022
Python
5
star
35

adventofcode2016

Python
5
star
36

coverage_pytest_plugin

Python
5
star
37

adventures_prz

A presentation
JavaScript
4
star
38

coverage-reports

HTML
4
star
39

injectx

Python
4
star
40

test0

PyCon presentation: Getting Started Testing
HTML
4
star
41

pgeom

Jupyter Notebook
4
star
42

toomuchregex

A lightning talk
JavaScript
4
star
43

native-matrix

4
star
44

templite

Python
4
star
45

typing_app

JavaScript
4
star
46

blowyournose

Python
3
star
47

stilted

Python
3
star
48

bigo

HTML
3
star
49

version_dummy

Absolute minimal versioned Python package.
Shell
3
star
50

jreport

Utility for making console reports from JSON APIs
Python
3
star
51

hello-github-actions

3
star
52

commitstats

Jupyter Notebook
3
star
53

natsworld

Python
3
star
54

point_match

A presentation.
HTML
3
star
55

blogtools

Python
3
star
56

explainer

3
star
57

tabtest

Python
3
star
58

branch-tests

2
star
59

toxghabug

2
star
60

xunit_tools

Janky tools for doing things with xunit.xml files
Python
2
star
61

sarai

HTML
2
star
62

madlib

Python
2
star
63

blank_prz

A blank presentation, using my own crappy toolchain
JavaScript
2
star
64

django_issue_25793

Demo for https://code.djangoproject.com/ticket/25793
Python
2
star
65

prznames

HTML
2
star
66

dinghy_sample

An example of publishing Dinghy digests
HTML
2
star
67

wikicrawl

Python
2
star
68

cyclorama

Python
1
star
69

adventofcode2023

Python
1
star
70

nedbat_dinghy

HTML
1
star
71

hackathon-edx-exams-copy

Python
1
star