diff --git a/AUTHORS b/AUTHORS index f06760e..25cde12 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,4 +1,5 @@ Carl J. Meyer Jannis Leidel +Omer Katz Russ Ferriday Rafal Stozek \ No newline at end of file diff --git a/README.rst b/README.rst index 33904f6..7230d6a 100644 --- a/README.rst +++ b/README.rst @@ -83,6 +83,9 @@ Setup - ``TEST_DISCOVER_PATTERN`` (optional) is the pattern to use when discovering 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 -------- @@ -131,6 +134,12 @@ Other than that it's similar to the new project's style configuration. Changelog --------- +0.4 XX/XX/XXXX +^^^^^^^^^^^^^^ + +* Added ability to override the discover settings with a custom test management + command. + 0.3 01/28/2013 ^^^^^^^^^^^^^^ diff --git a/discover_runner/management/commands/test.py b/discover_runner/management/commands/test.py index 7d91a4c..8522172 100644 --- a/discover_runner/management/commands/test.py +++ b/discover_runner/management/commands/test.py @@ -1,15 +1,21 @@ from optparse import make_option from django.core.management.commands.test import Command as TestCommand +from discover_runner import settings + + class Command(TestCommand): option_list = TestCommand.option_list + ( - make_option('--root', - action='store', dest='test_discover_root', default=None, - help='Overrides the TEST_DISCOVER_ROOT setting.'), - make_option('--top-level', - action='store', dest='test_discover_top_level', default=None, - help='Overrides the TEST_TOP_LEVEL setting.'), - make_option('--pattern', - action='store', dest='test_discover_pattern', default=None, - help='Overrides the TEST_DISCOVER_PATTERN setting.'), - ) \ No newline at end of file + make_option('-r', '--root', + action='store', dest='test_discover_root', + default=settings.TEST_DISCOVER_ROOT, + help='Overrides the TEST_DISCOVER_ROOT setting.'), + make_option('-t', '--top-level', + action='store', dest='test_discover_top_level', + default=settings.TEST_DISCOVER_TOP_LEVEL, + help='Overrides the TEST_TOP_LEVEL setting.'), + make_option('-p', '--pattern', + action='store', dest='test_discover_pattern', + default=settings.TEST_DISCOVER_PATTERN, + help='Overrides the TEST_DISCOVER_PATTERN setting.'), + ) diff --git a/discover_runner/runner.py b/discover_runner/runner.py index c6a923d..335494d 100644 --- a/discover_runner/runner.py +++ b/discover_runner/runner.py @@ -2,7 +2,10 @@ 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 -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: from django.utils.unittest import defaultTestLoader @@ -16,39 +19,34 @@ except ImportError: 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. - - """ + 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): suite = None - root = self.test_discover_root - top_level = self.test_discover_top_level - pattern = self.test_discover_pattern + root = self.discover_root 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 not suite.countTestCases() and len(test_labels) == 1: suite = None root = import_module(test_labels[0]).__path__[0] if suite is None: - suite = self.testLoader.discover(root, - pattern=pattern, top_level_dir=top_level) + suite = self.test_loader.discover(root, + pattern=self.discover_pattern, + top_level_dir=self.discover_top_level) if extra_tests: for test in extra_tests: