I have a Pyramid web application which uses Jinja2 as template engine. It is tested using the Pyramid testing helpers together with py.test and the coverage plugin. But coverage works only for my Python code. Is there way to check also for coverage of Jinja templates?
As Jinja is compiled into Python code and as Armin mananged to give exact line numbers in the case of an exception, I should at least be possible in theory. Any hint would be very appreciated.
Yes, it is possible to check for coverage of Jinja templates by using a coverage plugin specifically designed for Jinja templates, such as pytest-jinja-coverage. This plugin allows you to measure coverage of your Jinja templates by instrumenting the Jinja environment, and then reporting the coverage results just like you would for your Python code.
Here's an example of how you can use the plugin:
Install the plugin using pip:
pip install pytest-jinja-coverage
In your pytest.ini or setup.cfg file, configure the plugin to look for Jinja templates in the appropriate directory:
[jinja_coverage] template_dirs = myapp/templates
Run your test suite with coverage reporting enabled, for example:
pytest --cov-config .coveragerc --cov-report term-missing --cov=myapp
The plugin will instrument your Jinja templates, and the coverage report will include coverage statistics for both your Python code and your Jinja templates.
It is worth noting that this plugin relies on the Jinja template engine to provide line numbers for exceptions, so make sure that the Jinja environment is configured to do so. Also, this plugin is not a part of the official pytest plugins and it is maintained by an external group of developers, so you may want to check the plugin's documentation and recent updates to make sure it is still maintained.