django discover runner和XML报告?
我有一个正在运行的Django应用程序,它已成功地使用django discover runner和XML报告?,xml,django,unit-testing,Xml,Django,Unit Testing,我有一个正在运行的Django应用程序,它已成功地使用unittest xml reporting从我的unittests生成xml报告 然而,这个项目正在快速发展,我想在每个应用程序中将我的测试分解成单独的文件。因此,我安装了django discover runner,它可以找到我的所有测试文件并成功运行它们 但是,django discover runner不生成XML报告,这是我需要的(用于竹子) 我发现: 并尝试实现该建议(在我的每个test.py文件中),但没有生成XML 如何使用
unittest xml reporting
从我的unittests生成xml报告
然而,这个项目正在快速发展,我想在每个应用程序中将我的测试分解成单独的文件。因此,我安装了django discover runner
,它可以找到我的所有测试文件并成功运行它们
但是,django discover runner
不生成XML报告,这是我需要的(用于竹子)
我发现:
并尝试实现该建议(在我的每个test.py文件中),但没有生成XML
如何使用
django discover runner
和unittest xml reporting
来发现我的测试并生成xml报告?因此,解决这个问题的方法比我预期的要简单得多。对于可能希望这样做的任何其他N00B:
简而言之,我只是简单地将django discover runner
和unittest xml reporting
提供的两个测试运行程序拼凑到一个定制的测试运行程序中:
from django.conf import settings
from django.test.utils import setup_test_environment, teardown_test_environment
import xmlrunner
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner, reorder_suite
from django.utils.importlib import import_module
try:
from django.utils.unittest import defaultTestLoader
except ImportError:
try:
from unittest2 import defaultTestLoader # noqa
except ImportError:
raise ImproperlyConfigured("Couldn't import unittest2 default "
"test loader. Please use Django >= 1.3 "
"or go install the unittest2 library.")
### CUSTOM RUNNER NAME
class myTestRunner(DjangoTestSuiteRunner):
### THIS SECTION FROM UNITTESTS-XML-REPORTING
def build_suite(self, test_labels, extra_tests=None, **kwargs):
suite = None
root = getattr(settings, 'TEST_DISCOVER_ROOT', '.')
top_level = getattr(settings, 'TEST_DISCOVER_TOP_LEVEL', None)
pattern = getattr(settings, 'TEST_DISCOVER_PATTERN', 'test*.py')
if test_labels:
suite = defaultTestLoader.loadTestsFromNames(test_labels)
# if single named module has no tests, do discovery within it
if not suite.countTestCases() and len(test_labels) == 1:
suite = None
root = import_module(test_labels[0]).__path__[0]
if suite is None:
suite = defaultTestLoader.discover(root,
pattern=pattern, top_level_dir=top_level)
if extra_tests:
for test in extra_tests:
suite.addTest(test)
return reorder_suite(suite, (TestCase,))
###THIS SECTION FROM DJANGO-DISCOVER-RUNNER
def run_tests(self, test_labels, extra_tests=None, **kwargs):
"""
Run the unit tests for all the test labels in the provided list.
Labels must be of the form:
- app.TestClass.test_method
Run a single specific test method
- app.TestClass
Run all the test methods in a given class
- app
Search for doctests and unittests in the named application.
When looking for tests, the test runner will look in the models and
tests modules for the application.
A list of 'extra' tests may also be provided; these tests
will be added to the test suite.
Returns the number of tests that failed.
"""
setup_test_environment()
settings.DEBUG = False
verbosity = getattr(settings, 'TEST_OUTPUT_VERBOSE', 1)
if isinstance(verbosity, bool):
verbosity = (1, 2)[verbosity]
descriptions = getattr(settings, 'TEST_OUTPUT_DESCRIPTIONS', False)
output = getattr(settings, 'TEST_OUTPUT_DIR', '.')
suite = self.build_suite(test_labels, extra_tests)
old_config = self.setup_databases()
result = xmlrunner.XMLTestRunner(
verbosity=verbosity, descriptions=descriptions,
output=output).run(suite)
self.teardown_databases(old_config)
teardown_test_environment()
return len(result.failures) + len(result.errors)
这应该保存在项目中合适的地方。在测试设置文件(test\u settings.py
-根据django discover runner
说明)中,设置测试运行程序:
TEST_RUNNER = '<your-django-project>.customTestRunner.myTestRunner'
此解决方案允许我使用django discover runner
的功能来发现整个项目中的所有测试文件(由django discover runner
的test\u discover\u PATTERN
选项指定),并且仍然可以根据需要输出XML报告。非常感谢原始代码的作者:
自从提出这个问题以来,unittest xml报告项目已经为新的Django
DiscoveryRunner
类提供了支持。您只需在Django设置文件中设置测试运行程序:
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'
它将运行与DiscoverRunner相同的测试。谢谢!!配置它花了几秒钟的时间
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'