Ticket #16534: djangotestsuiterunner-2.diff

File djangotestsuiterunner-2.diff, 4.0 KB (added by tomchristie, 4 years ago)

Also allow test_runner to be overridden

  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index c9adfd2..a6491a1 100644
    a b def get_tests(app_module): 
    4949            raise
    5050    return test_module
    5151
    52 def build_suite(app_module):
     52def build_suite(app_module, test_loader=unittest.defaultTestLoader):
    5353    "Create a complete Django test suite for the provided application module"
    5454    suite = unittest.TestSuite()
    5555
    def build_suite(app_module): 
    5858    if hasattr(app_module, 'suite'):
    5959        suite.addTest(app_module.suite())
    6060    else:
    61         suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(app_module))
     61        suite.addTest(test_loader.loadTestsFromModule(app_module))
    6262        try:
    6363            suite.addTest(doctest.DocTestSuite(app_module,
    6464                                               checker=doctestOutputChecker,
    def build_suite(app_module): 
    7676        if hasattr(test_module, 'suite'):
    7777            suite.addTest(test_module.suite())
    7878        else:
    79             suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_module))
     79            suite.addTest(test_loader.loadTestsFromModule(test_module))
    8080            try:
    8181                suite.addTest(doctest.DocTestSuite(test_module,
    8282                                                   checker=doctestOutputChecker,
    def build_suite(app_module): 
    8686                pass
    8787    return suite
    8888
    89 def build_test(label):
     89def build_test(label, test_loader=unittest.defaultTestLoader):
    9090    """Construct a test case with the specified label. Label should be of the
    9191    form model.TestClass or model.TestClass.test_method. Returns an
    9292    instantiated test or test suite corresponding to the label provided.
    def build_test(label): 
    112112        if issubclass(TestClass, (unittest.TestCase, real_unittest.TestCase)):
    113113            if len(parts) == 2: # label is app.TestClass
    114114                try:
    115                     return unittest.TestLoader().loadTestsFromTestCase(TestClass)
     115                    return test_loader.loadTestsFromTestCase(TestClass)
    116116                except TypeError:
    117117                    raise ValueError("Test label '%s' does not refer to a test class" % label)
    118118            else: # label is app.TestClass.test_method
    def dependency_ordered(test_databases, dependencies): 
    220220        test_databases = deferred
    221221    return ordered_test_databases
    222222
     223
     224
    223225class DjangoTestSuiteRunner(object):
     226    suite_class = unittest.TestSuite
     227    runner_class = unittest.TextTestRunner
     228    test_loader = unittest.defaultTestLoader
     229
    224230    def __init__(self, verbosity=1, interactive=True, failfast=True, **kwargs):
    225231        self.verbosity = verbosity
    226232        self.interactive = interactive
    class DjangoTestSuiteRunner(object): 
    232238        unittest.installHandler()
    233239
    234240    def build_suite(self, test_labels, extra_tests=None, **kwargs):
    235         suite = unittest.TestSuite()
     241        suite = self.suite_class()
    236242
    237243        if test_labels:
    238244            for label in test_labels:
    239245                if '.' in label:
    240                     suite.addTest(build_test(label))
     246                    suite.addTest(build_test(label, self.test_loader))
    241247                else:
    242248                    app = get_app(label)
    243                     suite.addTest(build_suite(app))
     249                    suite.addTest(build_suite(app, self.test_loader))
    244250        else:
    245251            for app in get_apps():
    246                 suite.addTest(build_suite(app))
     252                suite.addTest(build_suite(app, self.test_loader))
    247253
    248254        if extra_tests:
    249255            for test in extra_tests:
    class DjangoTestSuiteRunner(object): 
    308314        return old_names, mirrors
    309315
    310316    def run_suite(self, suite, **kwargs):
    311         return unittest.TextTestRunner(verbosity=self.verbosity, failfast=self.failfast).run(suite)
     317        return self.runner_class(verbosity=self.verbosity, failfast=self.failfast).run(suite)
    312318
    313319    def teardown_databases(self, old_config, **kwargs):
    314320        from django.db import connections
Back to Top