~hruske Hruške, jabuke, jablane, čežane. » Blog Archive » Django and coverage.py, the DRY way
Home Contact Sitemap

Hruške, jabuke, jablane, čežane.

Ste se gdaj vprašali, zakaj Najboljšega soseda nikoli ni doma, ko pridete na obisk?

Django and coverage.py, the DRY way

Posted on September 25th, 2009 in django, python |

So you want to use Ned Batchelder’s coverage.py and don’t want to install another Django app, don’t want to patch Django files or you just can’t wait for bug #4501 to be resolved.

Me too.

So I took a couple of minutes and put together this very very DRY-ish djangocoverage module, which binds together coverage.py with Django. It is based on a bit aged Siddharta Govindaraj’s snippet. Initially I intended to monkeypatch Django from manage.py, but finding out about TEST_RUNNER setting, I made a saner decision and provided a test runner.

import coverage

def coverage_decorator(func, omit_prefixes=None):
 "Decorator for Django's built in test runner"
 def _inner_coverage(*args, **kwargs):
 from django.conf import settings
 c = coverage.coverage()
 c.erase()
 c.start()

 retval = func(*args, **kwargs)

 c.stop()

 coverage_dir = getattr(settings, 'COVERAGE_DIR', './coverage_results')
 c.html_report(directory=coverage_dir, omit_prefixes=omit_prefixes)
 return retval
 return _inner_coverage

def coverage_test_runner(*args, **kwargs):
 "Test runner you can put in your application's settings file"
 from django.test import simple
 return coverage_decorator(simple.run_tests)(*args, **kwargs)

What you want to do is put djangocoverage.py somewhere where Python can find it and put “TEST_RUNNER=’djangocoverage.coverage_test_runner’” line into your settings.py.

Then, running tests is the same as it was. If you’re already using custom test runner, then you can easily integrate coverage.py using decorator, if you haven’t yet been using coverage.py. HTML report is written out in ./coverage_report, and you can also adjust that via COVERAGE_DIR setting. Hope you find it helpful.

Reblog this post [with Zemanta]

Comments are closed.

Komentarji so izklopljeni