diff --git a/AUTHORS b/AUTHORS index b8c773b602..1a79d26297 100644 --- a/AUTHORS +++ b/AUTHORS @@ -252,6 +252,7 @@ answer newbie questions, and generally made Django that much better: james_027@yahoo.com jcrasta@gmail.com jdetaeye + Dmitry Jemerov jhenry john@calixto.net Zak Johnson @@ -338,6 +339,7 @@ answer newbie questions, and generally made Django that much better: mark@junklight.com Orestis Markou Takashi Matsuo + Zlatko Mašek Yasushi Masuda mattycakes@gmail.com Glenn Maynard @@ -369,6 +371,7 @@ answer newbie questions, and generally made Django that much better: Gopal Narayanan Fraser Nevett Sam Newman + Ryan Niemeyer Filip Noetzel Afonso Fernández Nogueira Neal Norwitz @@ -443,6 +446,7 @@ answer newbie questions, and generally made Django that much better: Pete Shinners Leo Shklovskii jason.sidabras@gmail.com + Mikołaj Siedlarek Brenton Simpson Jozko Skrablin Ben Slavin @@ -533,8 +537,6 @@ answer newbie questions, and generally made Django that much better: Gasper Zejn Jarek Zgoda Cheng Zhang - Zlatko Mašek - Ryan Niemeyer A big THANK YOU goes to: diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py index 1be1e1e768..1b3f2beada 100644 --- a/django/core/management/commands/test.py +++ b/django/core/management/commands/test.py @@ -1,29 +1,56 @@ +from django.conf import settings from django.core.management.base import BaseCommand -from optparse import make_option +from optparse import make_option, OptionParser import sys +from django.test.utils import get_runner class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), make_option('--failfast', action='store_true', dest='failfast', default=False, - help='Tells Django to stop running the test suite after first failed test.') + help='Tells Django to stop running the test suite after first failed test.'), + make_option('--testrunner', action='store', dest='testrunner', + help='Tells Django to use specified test runner class instead of the one '+ + 'specified by the TEST_RUNNER setting.') ) help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' args = '[appname ...]' requires_model_validation = False + def run_from_argv(self, argv): + """ + Pre-parse the command line to extract the value of the --testrunner + option. This allows a test runner to define additional command line + arguments. + """ + self.test_runner = None + option = '--testrunner=' + for arg in argv[2:]: + if arg.startswith(option): + self.test_runner = arg[len(option):] + break + super(Command, self).run_from_argv(argv) + + def create_parser(self, prog_name, subcommand): + test_runner_class = get_runner(settings, self.test_runner) + options = self.option_list + getattr(test_runner_class, 'option_list', ()) + return OptionParser(prog=prog_name, + usage=self.usage(subcommand), + version=self.get_version(), + option_list=options) + def handle(self, *test_labels, **options): from django.conf import settings from django.test.utils import get_runner - verbosity = int(options.get('verbosity', 1)) - interactive = options.get('interactive', True) - failfast = options.get('failfast', False) - TestRunner = get_runner(settings) + TestRunner = get_runner(settings, options.get('testrunner')) + options['verbosity'] = int(options.get('verbosity', 1)) + options.setdefault('interactive', True) + options.setdefault('failfast', False) - test_runner = TestRunner(verbosity=verbosity, interactive=interactive, failfast=failfast) + test_runner = TestRunner(**options) failures = test_runner.run_tests(test_labels) if failures: diff --git a/django/test/utils.py b/django/test/utils.py index c394dacfd6..b9f4743a34 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -118,8 +118,11 @@ def restore_warnings_state(state): warnings.filters = state[:] -def get_runner(settings): - test_path = settings.TEST_RUNNER.split('.') +def get_runner(settings, test_runner_class=None): + if not test_runner_class: + test_runner_class = settings.TEST_RUNNER + + test_path = test_runner_class.split('.') # Allow for Python 2.5 relative paths if len(test_path) > 1: test_module_name = '.'.join(test_path[:-1]) diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 6aac4ced92..46b7307613 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -964,6 +964,13 @@ information. Use the :djadminopt:`--failfast` option to stop running tests and report the failure immediately after a test fails. +.. versionadded:: 1.4 +.. django-admin-option:: --testrunner + +The :djandminopt:`--testrunner` option can be used to control the test runner +class that is used to execute tests. If this value is provided, it overrides +the value provided by the :setting:`TEST_RUNNER` setting. + testserver -------------------------------- diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt index a21ad6a77e..6595c51957 100644 --- a/docs/topics/testing.txt +++ b/docs/topics/testing.txt @@ -1744,6 +1744,29 @@ set up, execute and tear down the test suite. write your own test runner, ensure accept and handle the ``**kwargs`` parameter. + .. versionadded:: 1.4 + + Your test runner may also define additional command-line options. + If you add an ``option_list`` attribute to a subclassed test runner, + those options will be added to the list of command-line options that + the :djadmin:`test` command can use. + + +Attributes +~~~~~~~~~~ + + +.. attribute:: DjangoTestSuiteRunner.option_list + + .. versionadded:: 1.4 + + This is the tuple of ``optparse`` options which will be fed into the + management command's ``OptionParser`` for parsing arguments. See the + documentation for Python's ``optparse`` module for more details. + +Methods +~~~~~~~ + .. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs) Run the test suite. diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py index b3d6475697..4261e44d7f 100644 --- a/tests/regressiontests/test_runner/tests.py +++ b/tests/regressiontests/test_runner/tests.py @@ -2,12 +2,15 @@ Tests for django test runner """ import StringIO +from optparse import make_option import warnings from django.core.exceptions import ImproperlyConfigured +from django.core.management import call_command from django.test import simple from django.test.utils import get_warnings_state, restore_warnings_state from django.utils import unittest +from regressiontests.admin_scripts.tests import AdminScriptTestCase class DjangoTestRunnerTests(unittest.TestCase): @@ -128,3 +131,75 @@ class DependencyOrderingTests(unittest.TestCase): self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies) + +class MockTestRunner(object): + invoked = False + + def __init__(self, *args, **kwargs): + pass + + def run_tests(self, test_labels, extra_tests=None, **kwargs): + MockTestRunner.invoked = True + + +class ManageCommandTests(unittest.TestCase): + + def test_custom_test_runner(self): + call_command('test', 'sites', + testrunner='regressiontests.test_runner.tests.MockTestRunner') + self.assertTrue(MockTestRunner.invoked, + "The custom test runner has not been invoked") + + +class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner): + option_list = ( + make_option('--option_a','-a', action='store', dest='option_a', default='1'), + make_option('--option_b','-b', action='store', dest='option_b', default='2'), + make_option('--option_c','-c', action='store', dest='option_c', default='3'), + ) + + def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs): + super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive, + failfast=failfast) + self.option_a = option_a + self.option_b = option_b + self.option_c = option_c + + def run_tests(self, test_labels, extra_tests=None, **kwargs): + print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c) + + +class CustomTestRunnerOptionsTests(AdminScriptTestCase): + + def setUp(self): + settings = { + 'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'', + } + self.write_settings('settings.py', sdict=settings) + + def tearDown(self): + self.remove_settings('settings.py') + + def test_default_options(self): + args = ['test', '--settings=settings'] + out, err = self.run_django_admin(args) + self.assertNoOutput(err) + self.assertOutput(out, '1:2:3') + + def test_default_and_given_options(self): + args = ['test', '--settings=settings', '--option_b=foo'] + out, err = self.run_django_admin(args) + self.assertNoOutput(err) + self.assertOutput(out, '1:foo:3') + + def test_option_name_and_value_separated(self): + args = ['test', '--settings=settings', '--option_b', 'foo'] + out, err = self.run_django_admin(args) + self.assertNoOutput(err) + self.assertOutput(out, '1:foo:3') + + def test_all_options_given(self): + args = ['test', '--settings=settings', '--option_a=bar', '--option_b=foo', '--option_c=31337'] + out, err = self.run_django_admin(args) + self.assertNoOutput(err) + self.assertOutput(out, 'bar:foo:31337')