2010-11-23 91 views
7

我想用coveragedjango上运行测试。它工作正常,但它不检测类定义,因为它们是在覆盖开始之前定义的。我有以下的测试运行,我使用,我计算范围:django:覆盖范围的运行测试

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) 

我能做些什么,有类也纳入覆盖范围?否则,我的覆盖率相当低,我不能轻松检测到地点,这确实需要加以保护。

回答

8

最简单的做法是使用coverage来执行测试运行器。如果你的跑步者被称为“runner.py”,然后使用:

coverage run runner.py 

你可以把你的四个排除为.coveragerc文件,你就会有所有的你的代码覆盖率的好处,不保持任何的您的覆盖代码。

相关问题