django:运行带覆盖的测试

gru*_*czy 8 testing django code-coverage

我想在运行测试djangocoverage.它工作正常,但它不检测类定义,因为它们是在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)
Run Code Online (Sandbox Code Playgroud)

我能做些什么,课程中还包括课程?否则我的覆盖率很低,我无法轻易发现真正需要覆盖的地方.

Ned*_*der 9

最简单的方法是使用coverage来执行测试运行器.如果你的跑步者被称为"runner.py",那么使用:

coverage run runner.py
Run Code Online (Sandbox Code Playgroud)

您可以将四个排除项放入一个.coveragerc文件中,您将获得保险代码的所有好处,而无需保留任何承保范围代码.