【问题标题】:django: running tests with coveragedjango:运行覆盖率测试
【发布时间】:2010-11-23 11:47:07
【问题描述】:

我正在尝试使用 coverage 在 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)

我该怎么做才能让课程也包含在覆盖范围内?否则我的覆盖率很低,我无法轻易检测到真正需要覆盖的地方。

【问题讨论】:

    标签: django testing code-coverage


    【解决方案1】:

    最简单的做法是使用覆盖率来执行测试运行程序。如果你的跑步者被称为“runner.py”,那么使用:

    coverage run runner.py
    

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

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多