Login

Class-based coverage test runner

Author:
brutasse
Posted:
June 4, 2010
Language:
Python
Version:
1.2
Score:
1 (after 1 ratings)

A coverage test runner that uses the class-based runner introduced with Django 1.2.

Put it in your python path and add to your settings.py:

TEST_RUNNER = 'path_to.CoverageRunner'

COVERAGE_MODULES = [
    'blog.views',
    'projects.views',
    'middleware',
]

Compatible with Django 1.2 and higher. You also need Ned Batchelder's coverage.py module (pip install coverage).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# -*- coding: utf-8 -*-
from django.conf import settings
from django.test.simple import DjangoTestSuiteRunner

import coverage


class CoverageRunner(DjangoTestSuiteRunner):

    def run_tests(self, *args, **kwargs):
        run_with_coverage = hasattr(settings, 'COVERAGE_MODULES')

        if run_with_coverage:
            coverage.use_cache(0)
            coverage.start()

        result = super(CoverageRunner, self).run_tests(*args, **kwargs)

        if run_with_coverage:
            coverage.stop()
            print ''
            print '----------------------------------------------------------------------'
            print ' Unit Test Code Coverage Results'
            print '----------------------------------------------------------------------'
            coverage_modules = []
            for module in settings.COVERAGE_MODULES:
                coverage_modules.append(__import__(module, globals(),
                                                   locals(), ['']))
            coverage.report(coverage_modules, show_missing=1)
            print '----------------------------------------------------------------------'

        return result

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.