django:使用覆盖率运行测试

django:使用覆盖率运行测试,django,testing,code-coverage,Django,Testing,Code Coverage,我正在尝试在django上运行测试,覆盖率coverage。它可以正常工作,但不会检测类定义,因为它们是在覆盖开始之前定义的。我在计算覆盖率时使用以下测试运行程序: import sys import os import logging from django.conf import settings MAIN_TEST_RUNNER = 'django.test.simple.run_tests' if settings.COMPUTE_COVERAGE: try:

我正在尝试在
django
上运行测试,覆盖率
coverage
。它可以正常工作,但不会检测类定义,因为它们是在覆盖开始之前定义的。我在计算覆盖率时使用以下测试运行程序:

import sys
import os
import logging

from django.conf import settings

MAIN_TEST_RUNNER = 'django.test.simple.run_tests'

if settings.COMPUTE_COVERAGE:
    try:
        import coverage
    except ImportError:
        print "Warning: coverage module not found: test code coverage will not be computed"
    else:
        coverage.exclude('def __unicode__')
        coverage.exclude('if DEBUG')
        coverage.exclude('if settings.DEBUG')
        coverage.exclude('raise')
        coverage.erase()
        coverage.start()
        MAIN_TEST_RUNNER = 'django-test-coverage.runner.run_tests'

def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
    # start coverage - jeśli włączmy już tutaj, a wyłączymy w django-test-coverage,
    # to dostaniemy dobrze wyliczone pokrycie dla instrukcji wykonywanych przy
    # imporcie modułów
    test_path = MAIN_TEST_RUNNER.split('.')
    # Allow for Python 2.5 relative paths
    if len(test_path) > 1:
        test_module_name = '.'.join(test_path[:-1])
    else:
        test_module_name = '.'
    test_module = __import__(test_module_name, {}, {}, test_path[-1])
    test_runner = getattr(test_module, test_path[-1])
    failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive)
    if failures:
        sys.exit(failures)

我能做什么,让课程也包括在覆盖范围内?否则,我的覆盖率很低,并且我无法轻松检测到真正需要覆盖的位置。

最简单的方法是使用覆盖率来执行测试运行程序。如果您的跑步者名为“runner.py”,则使用:

coverage run runner.py
您可以将您的四项除外责任放入
.coverage.c
文件中,您将获得您的保险代码的所有好处,而无需保留任何保险代码