Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage for jinja2 templates in Django

Following Ned Batchelder's Coverage.py for Django templates blog post and the django_coverage_plugin plugin for measuring code coverage of Django templates.

I would really like to see template coverage reports, but the problem is - we have replaced the Django's the template engine with jinja2 through the coffin adapter. I know it is not recommended, but we had reasons. Basically, instead of Django's render_to_response() we are using:

from coffin.shortcuts import render_to_response

Currently, we are using django-coverage's test_coverage command to run tests with coverage:

python manage.py test_coverage project_name

I understand that the setup is not common. But, is it possible to gather code coverage metrics for jinja2 templates with coverage.py? If not, I would appreciate any ideas on making a plugin similar to django_coverage_plugin, but for jinja2.

like image 907
alecxe Avatar asked Jan 19 '15 22:01

alecxe


People also ask

What is code coverage Django?

Code coverage is a simple tool for checking which lines of your application code are run by your test suite. 100% coverage is a laudable goal, as it means every line is run at least once. Coverage.py is the Python tool for measuring code coverage.

Does Django use Jinja2?

Django sets Jinja with two default behaviors, one for when DEBUG=True -- a common setting in development -- and the other for when DEBUG=False -- a common setting in production. If DEBUG=True and an invalid variable is set in a Jinja template, Jinja uses the jinja2.


1 Answers

The plugin support in coverage.py is still in an alpha state. I've implemented the Django template support and half of the Mako support. A Jinja plugin would be appreciated. It might need changes in Jinja to make it feasible, in particular, to map the compiled Python code execution back to template line numbers.

If you want to start work on it, read the interfaces in plugin.py in the coverage.py source (I'll eventually write real docs, of course). The source for the Mako plugin might help also: https://bitbucket.org/ned/coverage-mako-plugin

This will likely involve us working together to get past the rough parts. Drop me an email! :)

Thanks for taking it on!

Updated: I've written some docs for the plugins, though more are needed: http://coverage.readthedocs.org/en/coverage-4.0a4/plugins.html

like image 104
Ned Batchelder Avatar answered Oct 01 '22 08:10

Ned Batchelder