diff --git a/django/test/runner.py b/django/test/runner.py index 84fe249..5ff3f15 100644 --- a/django/test/runner.py +++ b/django/test/runner.py @@ -14,6 +14,8 @@ class DiscoverRunner(object): A Django test runner that uses unittest2 test discovery. """ + test_suite = TestSuite + test_runner = unittest.TextTestRunner test_loader = defaultTestLoader reorder_by = (TestCase, ) option_list = ( @@ -42,7 +44,7 @@ class DiscoverRunner(object): unittest.installHandler() def build_suite(self, test_labels=None, extra_tests=None, **kwargs): - suite = TestSuite() + suite = self.test_suite() test_labels = test_labels or ['.'] extra_tests = extra_tests or [] @@ -107,7 +109,7 @@ class DiscoverRunner(object): return setup_databases(self.verbosity, self.interactive, **kwargs) def run_suite(self, suite, **kwargs): - return unittest.TextTestRunner( + return self.test_runner( verbosity=self.verbosity, failfast=self.failfast, ).run(suite) @@ -201,7 +203,8 @@ def reorder_suite(suite, classes): classes[1], etc. Tests with no match in classes are placed last. """ class_count = len(classes) - bins = [unittest.TestSuite() for i in range(class_count+1)] + suite_class = type(suite) + bins = [suite_class() for i in range(class_count+1)] partition_suite(suite, classes, bins) for i in range(class_count): bins[0].addTests(bins[i+1]) diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 32b5e91..326986c 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -276,6 +276,16 @@ Templates :setting:`TEMPLATE_DEBUG` is ``True``. This allows template origins to be inspected and logged outside of the ``django.template`` infrastructure. +Tests +^^^^^ + +* :class:`~django.test.runner.DiscoverRunner` has two new attributes, + :attr:`test_suite` and :attr:`test_runner`, which facilitate + overriding the way tests are collected and run. Documentation has been + added for the similar :attr:`test_loader` attribute + which has been in place since 1.6. + + Backwards incompatible changes in 1.7 ===================================== diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index d8d59c6..2195f9a 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -299,13 +299,15 @@ Defining a test runner .. currentmodule:: django.test.runner A test runner is a class defining a ``run_tests()`` method. Django ships -with a ``DiscoverRunner`` class that defines the default Django -testing behavior. This class defines the ``run_tests()`` entry point, -plus a selection of other methods that are used to by ``run_tests()`` to -set up, execute and tear down the test suite. +with a ``DiscoverRunner`` class that defines the default Django testing +behavior. This class defines the ``run_tests()`` entry point, plus a +selection of other methods that are used to by ``run_tests()`` to set up, +execute and tear down the test suite. .. class:: DiscoverRunner(pattern='test*.py', top_level=None, verbosity=1, interactive=True, failfast=True, **kwargs) +.. versionadded:: 1.6 + ``DiscoverRunner`` will search for tests in any file matching ``pattern``. ``top_level`` can be used to specify the directory containing your @@ -339,6 +341,34 @@ set up, execute and tear down the test suite. Attributes ~~~~~~~~~~ +.. attribute:: DiscoverRunner.test_suite + +.. versionadded:: 1.7 + + The class used to build the test suite. By default it is set to + ``unittest.TestSuite``. This can be overridden if you wish to implement + different logic for collecting tests into a test suite. + +.. attribute:: DiscoverRunner.test_runner + +.. versionadded:: 1.7 + + This is the class of the low-level test runner which is used to execute + the individual tests and format the results. By default it is set to + ``unittest.TextTestRunner``. Despite the unfortunate similarity in + naming conventions, this is not the same type of class as + ``DiscoverRunner``, which covers a broader set of responsibilites. You + can override this attribute to modify the way tests are run and reported. + +.. attribute:: DiscoverRunner.test_loader + +.. versionadded:: 1.6 + + This is the class that loads tests, whether from TestCases or modules or + otherwise and bundles them into test suites for the runner to execute. + By default it is set to ``unittest.defaultTestLoader``. You can override + this attribute if your tests are going to be loaded in unusual ways. + .. attribute:: DiscoverRunner.option_list This is the tuple of ``optparse`` options which will be fed into the