2013-02-23 32 views
5

我有一個正在使用的Django應用程序,它已成功使用unittest-xml-reporting從我的unittests生成XML報告。django-discover-runner和XML報告?

但是,該項目正在快速增長,我想將測試分解成每個應用程序內的單獨文件。因此,我安裝了django-discover-runner,它找到我所有的測試文件併成功運行它們。

但是,django-discover-runner不生成XML報告,我需要(用於Bamboo)。

我發現這一點:

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

,並嘗試實施這一建議(在我的每一個test.py文件),但沒有XML產生。

如何使用django-discover-runnerunittest-xml-reporting來發現我的測試並生成XML報告?

回答

3

所以事實證明,解決這個問題比我想象的要容易得多。對於誰可能要做到這一點任何其他n00bs:

簡短的回答是,我只是鵝卵石通過django-discover-runnerunittest-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_settings.py - 按django-discover-runner指令),設定測試運行:

TEST_RUNNER = '<your-django-project>.customTestRunner.myTestRunner' 

你會再使用(再一次,爲每django-discover-runner指令):

django-admin.py test --settings=myapp.test_settings 

該解決方案允許我使用django-discover-runner的功能來發現我的項目中的所有測試文件 - 由django-discover-runnerTEST_DISCOVER_PATTERN選項指定 - 並且仍按照Bamboo的要求輸出XML報告。非常感謝原代碼的作者:

django-discover-runner

unittest-xml-reports

3

因爲這個問題被問時,單元測試,XML的報告項目有added support新的Django DiscoverRunner類。你可以只設置測試運行在你的Django settings文件:

TEST_RUNNER = 'xmlrunner.extra.djangotestrunner.XMLTestRunner' 

它將運行相同的測試作爲DiscoverRunner會。