Further cleanup of the test management command.

This commit is contained in:
Jannis Leidel
2013-03-24 20:04:40 +01:00
parent 700864c340
commit 100de1d7cd
4 changed files with 44 additions and 30 deletions

View File

@@ -1,4 +1,5 @@
Carl J. Meyer Carl J. Meyer
Jannis Leidel Jannis Leidel
Omer Katz
Russ Ferriday Russ Ferriday
Rafal Stozek Rafal Stozek

View File

@@ -83,6 +83,9 @@ Setup
- ``TEST_DISCOVER_PATTERN`` (optional) is the pattern to use when discovering - ``TEST_DISCOVER_PATTERN`` (optional) is the pattern to use when discovering
tests and defaults to the unittest2_ standard ``test*.py``. tests and defaults to the unittest2_ standard ``test*.py``.
- Add `'discover_runner'` to your ``INSTALLED_APPS`` setting to enable the
ability to override the settings above using the ``test`` management command.
Examples Examples
-------- --------
@@ -131,6 +134,12 @@ Other than that it's similar to the new project's style configuration.
Changelog Changelog
--------- ---------
0.4 XX/XX/XXXX
^^^^^^^^^^^^^^
* Added ability to override the discover settings with a custom test management
command.
0.3 01/28/2013 0.3 01/28/2013
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

View File

@@ -1,15 +1,21 @@
from optparse import make_option from optparse import make_option
from django.core.management.commands.test import Command as TestCommand from django.core.management.commands.test import Command as TestCommand
from discover_runner import settings
class Command(TestCommand): class Command(TestCommand):
option_list = TestCommand.option_list + ( option_list = TestCommand.option_list + (
make_option('--root', make_option('-r', '--root',
action='store', dest='test_discover_root', default=None, action='store', dest='test_discover_root',
help='Overrides the TEST_DISCOVER_ROOT setting.'), default=settings.TEST_DISCOVER_ROOT,
make_option('--top-level', help='Overrides the TEST_DISCOVER_ROOT setting.'),
action='store', dest='test_discover_top_level', default=None, make_option('-t', '--top-level',
help='Overrides the TEST_TOP_LEVEL setting.'), action='store', dest='test_discover_top_level',
make_option('--pattern', default=settings.TEST_DISCOVER_TOP_LEVEL,
action='store', dest='test_discover_pattern', default=None, help='Overrides the TEST_TOP_LEVEL setting.'),
help='Overrides the TEST_DISCOVER_PATTERN setting.'), make_option('-p', '--pattern',
) action='store', dest='test_discover_pattern',
default=settings.TEST_DISCOVER_PATTERN,
help='Overrides the TEST_DISCOVER_PATTERN setting.'),
)

View File

@@ -2,7 +2,10 @@ from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner, reorder_suite from django.test.simple import DjangoTestSuiteRunner, reorder_suite
from django.utils.importlib import import_module from django.utils.importlib import import_module
from settings import TEST_DISCOVER_ROOT, TEST_DISCOVER_TOP_LEVEL, TEST_DISCOVER_PATTERN
from discover_runner.settings import (TEST_DISCOVER_ROOT,
TEST_DISCOVER_TOP_LEVEL,
TEST_DISCOVER_PATTERN)
try: try:
from django.utils.unittest import defaultTestLoader from django.utils.unittest import defaultTestLoader
@@ -16,39 +19,34 @@ except ImportError:
class DiscoverRunner(DjangoTestSuiteRunner): class DiscoverRunner(DjangoTestSuiteRunner):
testLoader = defaultTestLoader
reorder_by = (TestCase,)
def __init__(self, test_discover_root=None, test_discover_top_level=None, test_discover_pattern=None, *args,
**kwargs):
self.test_discover_root = test_discover_root or TEST_DISCOVER_ROOT
self.test_discover_top_level = test_discover_top_level or TEST_DISCOVER_TOP_LEVEL
self.test_discover_pattern = test_discover_pattern or TEST_DISCOVER_PATTERN
super(DiscoverRunner, self).__init__(*args, **kwargs)
""" """
A test suite runner that uses unittest2 test discovery. A test suite runner that uses unittest2 test discovery.
""" """
test_loader = defaultTestLoader
reorder_by = (TestCase,)
def __init__(self, discover_root=None, discover_top_level=None,
discover_pattern=None, *args, **kwargs):
super(DiscoverRunner, self).__init__(*args, **kwargs)
self.discover_root = discover_root or TEST_DISCOVER_ROOT
self.discover_top_level = discover_top_level or TEST_DISCOVER_TOP_LEVEL
self.discover_pattern = discover_pattern or TEST_DISCOVER_PATTERN
def build_suite(self, test_labels, extra_tests=None): def build_suite(self, test_labels, extra_tests=None):
suite = None suite = None
root = self.test_discover_root root = self.discover_root
top_level = self.test_discover_top_level
pattern = self.test_discover_pattern
if test_labels: if test_labels:
suite = self.testLoader.loadTestsFromNames(test_labels) suite = self.test_loader.loadTestsFromNames(test_labels)
# if single named module has no tests, do discovery within it # if single named module has no tests, do discovery within it
if not suite.countTestCases() and len(test_labels) == 1: if not suite.countTestCases() and len(test_labels) == 1:
suite = None suite = None
root = import_module(test_labels[0]).__path__[0] root = import_module(test_labels[0]).__path__[0]
if suite is None: if suite is None:
suite = self.testLoader.discover(root, suite = self.test_loader.discover(root,
pattern=pattern, top_level_dir=top_level) pattern=self.discover_pattern,
top_level_dir=self.discover_top_level)
if extra_tests: if extra_tests:
for test in extra_tests: for test in extra_tests: