首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django-discover runner和XML报告

django-discover runner和XML报告
EN

Stack Overflow用户
提问于 2013-02-23 17:26:30
回答 2查看 3K关注 0票数 10

我有一个可以工作的Django应用程序,它已经成功地使用unittest-xml-reporting从我的单元测试中生成XML报告。

然而,这个项目正在迅速发展,我想将我的测试分解到每个应用程序中的单独文件中。因此,我安装了django-discover-runner,它可以找到我所有的测试文件并成功地运行它们。

但是,django-discover-runner不会生成我需要的报告(对于竹子)。

我发现了这个:

http://www.stevetrefethen.com/blog/Publishing-Python-unit-test-results-in-Jenkins.aspx

并尝试实现该建议(在我的每个test.py文件中),但是没有生成任何XML。

如何同时使用django-discover-runnerunittest-xml-reporting来发现我的测试并生成XML报告?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-25 11:39:45

所以事实证明,解决这个问题比我预期的要容易得多。对于可能想要这样做的任何其他n00bs:

简单地说,我只是将django-discover-runnerunittest-xml-reporting提供的两个测试运行器拼凑成一个自定义的测试运行器:

代码语言:javascript
复制
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_settings.py -根据django-discover-runner说明)中,设置测试运行器:

代码语言:javascript
复制
TEST_RUNNER = '<your-django-project>.customTestRunner.myTestRunner'

然后,您将使用(同样,根据django-discover-runner说明):

代码语言:javascript
复制
django-admin.py test --settings=myapp.test_settings

这个解决方案允许我使用django-discover-runner的特性来发现我的项目中的所有测试文件--由django-discover-runnerTEST_DISCOVER_PATTERN选项指定--并且仍然按照竹子的要求输出XML报告。

django-discover-runner

unittest-xml-reports

票数 5
EN

Stack Overflow用户

发布于 2015-04-22 06:06:20

由于提出了这个问题,unittest-xml-reporting项目具有新的Django DiscoverRunner类的added support。您可以在Django设置文件中设置测试运行器:

代码语言:javascript
复制
TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner'

它将运行与DiscoverRunner相同的测试。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15039013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档