Code coverage for jinja2 templates in Django - python

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.

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

Related

Finding source of manage.py subcommand

I'm new to django and working on a modern django/Wagtail CMS app that is seeded using the command python manage.py loadfixtures. It seems like loaddata is the more common command, and I'm finding it incredibly difficult to find any documentation on loadfixtures at all. Could anyone point me towards the difference? It appears under the [Core] section of "available subcommands."
As an aside, I'm essentially trying to dump and seed some static page data for the site.
loadfixtures is not a command that exists as standard on either Django or Wagtail, so it's presumably a custom command that's been added by a developer on your project, or possibly a third-party package you have installed. See Writing custom django-admin commands for documentation on how these are created - if you have a 'core' app as part of your project, you may well find the code for the command in core/management/commands/loadfixtures.py.

Use Pytest with Django without using django-pytest 3rd party app

Is it possible to use Pytest with Django without using django-pytest 3rd party app?
I had tried to set this up, but kept running into random errors, like pytest couldn't find the DJANGO_SETTINGS_MODULE. Then I fixed the path, but the normal python manage.py runserver then couldn't find the DJANGO_SETTINGS_MODULE. I'm running:
Pytest 2.5.4
Python 3.4.0
Django 1.6.2
If it is possible, would you be able to provide a setup example of where to put the tests/ directory, etc... within the project so Pytest works?
Thanks
Hmm, py.test 2.5.4 does not exist afaik.
Anyway, assuming you mean to ask whether it is possible to avoid the pytest-django plugin to test Django using py.test: the short answer is no.
The long answer is yes, but it is extremely difficult to get it all to work and you will basically write at least a minimal version of pytest-django in the conftest.py file. The pytest-django plugin was created specifically to work around all the weirdness which Django does with much global state and other hidden magic.
OTOH looking at the pytest-django source would probably help you kickstart such an effort. However you may consider thinking about what it is of pytest-django you do not like and maybe file an enhancement request to improve it.

Pycharm resolving references in Django templates

Is it possible for PyCharm to resolve references in Django templates? i.e., some code completion support, and when Ctrl + click on a template variable, we should go to the relevant python object.
The Jetbrains website boasts some impressive magic about Django templates:
Code completion works for Django tags, filters, template variables
and parameters passed from view methods.
The review on Dr Dobbs confirms this functionality. But I can't seem to get it to work:
I have setup the correct interpreter from my virtualenv, enabled Django Support in the settings and configured the templates directories, even invalidated and rebuilt the caches.
I'm on PyCharm v3.0.2.
In order to get this kind of functionality, you will need to see an icon on your views.py file, of the corresponding html file:
Using 3.0.2 here:
Either way, I will need to have a look at your directory and views.py file before I can tell you whats wrong with your source.
I had the same problem and I solved it by figuring out that my settings.py file was not configured in File > Settings > Languages & Frameworks > Django. Once I did that all intellisense clicked into place.
I've heard that this is a Pro-only feature but I cannot confirm that as I'm using a Pro license.

Code coverage for Jinja2 templates

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.
There is jinja_coverage.
However, it is incomplete and has a single developer with very little activity (not even published yet). You could also just take its code as hints for how to do it yourself.

Finding unused Django code to remove

I've started working on a project with loads of unused legacy code in it. I was wondering if it might be possible to use a tool like coverage in combination with a crawler (like the django-test-utils one) to help me locate code which isn't getting hit which we can mark with deprecation warnings. I realise that something like this won't be foolproof but thought it might help.
I've tried running coverage.py with the django debug server but it doesn't work correctly (it seems to just profile the runserver machinery rather than my views, etc).
We're improving our test coverage all the time but there's a way to go and I thought there might be a quicker way.
Any thoughts?
Thanks.
You can run the development server under coverage if you use the --noreload switch:
coverage run ./manage.py runserver --noreload
pylint is great tool for static code analysis (among others things it will detect unused imports, variables or arguments).
http://nedbatchelder.com/blog/200806/pylint.html
http://www.doughellmann.com/articles/pythonmagazine/completely-different/2008-03-linters/index.html

Categories

Resources