Author: russellm
Date: 2011-06-10 01:26:05 -0700 (Fri, 10 Jun 2011)
New Revision: 16352

Modified:
   django/trunk/AUTHORS
   django/trunk/django/core/management/commands/test.py
   django/trunk/django/test/utils.py
   django/trunk/docs/ref/django-admin.txt
   django/trunk/docs/topics/testing.txt
   django/trunk/tests/regressiontests/test_runner/tests.py
Log:
Fixed #16185, #15675 -- Added the ability for test runners to define custom 
options, and to specify a custom test runner at the command line. Thanks to 
Dmitry Jemerov and Miko?\197?\130aj Siedlarek for the patch.

Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2011-06-09 23:51:03 UTC (rev 16351)
+++ django/trunk/AUTHORS        2011-06-10 08:26:05 UTC (rev 16352)
@@ -252,6 +252,7 @@
     [email protected]
     [email protected]
     jdetaeye
+    Dmitry Jemerov <[email protected]>
     jhenry <[email protected]>
     [email protected]
     Zak Johnson <[email protected]>
@@ -338,6 +339,7 @@
     [email protected]
     Orestis Markou <[email protected]>
     Takashi Matsuo <[email protected]>
+    Zlatko Mašek <[email protected]>
     Yasushi Masuda <[email protected]>
     [email protected]
     Glenn Maynard <[email protected]>
@@ -369,6 +371,7 @@
     Gopal Narayanan <[email protected]>
     Fraser Nevett <[email protected]>
     Sam Newman <http://www.magpiebrain.com/>
+    Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
     Filip Noetzel <http://filip.noetzel.co.uk/>
     Afonso Fernández Nogueira <[email protected]>
     Neal Norwitz <[email protected]>
@@ -443,6 +446,7 @@
     Pete Shinners <[email protected]>
     Leo Shklovskii
     [email protected]
+    Mikołaj Siedlarek <[email protected]>
     Brenton Simpson <http://theillustratedlife.com>
     Jozko Skrablin <[email protected]>
     Ben Slavin <[email protected]>
@@ -533,8 +537,6 @@
     Gasper Zejn <[email protected]>
     Jarek Zgoda <[email protected]>
     Cheng Zhang
-    Zlatko Mašek <[email protected]>
-    Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
 
 A big THANK YOU goes to:
 

Modified: django/trunk/django/core/management/commands/test.py
===================================================================
--- django/trunk/django/core/management/commands/test.py        2011-06-09 
23:51:03 UTC (rev 16351)
+++ django/trunk/django/core/management/commands/test.py        2011-06-10 
08:26:05 UTC (rev 16352)
@@ -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:

Modified: django/trunk/django/test/utils.py
===================================================================
--- django/trunk/django/test/utils.py   2011-06-09 23:51:03 UTC (rev 16351)
+++ django/trunk/django/test/utils.py   2011-06-10 08:26:05 UTC (rev 16352)
@@ -118,8 +118,11 @@
     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])

Modified: django/trunk/docs/ref/django-admin.txt
===================================================================
--- django/trunk/docs/ref/django-admin.txt      2011-06-09 23:51:03 UTC (rev 
16351)
+++ django/trunk/docs/ref/django-admin.txt      2011-06-10 08:26:05 UTC (rev 
16352)
@@ -964,6 +964,13 @@
 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 <fixture fixture ...>
 --------------------------------
 

Modified: django/trunk/docs/topics/testing.txt
===================================================================
--- django/trunk/docs/topics/testing.txt        2011-06-09 23:51:03 UTC (rev 
16351)
+++ django/trunk/docs/topics/testing.txt        2011-06-10 08:26:05 UTC (rev 
16352)
@@ -1744,6 +1744,29 @@
     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.

Modified: django/trunk/tests/regressiontests/test_runner/tests.py
===================================================================
--- django/trunk/tests/regressiontests/test_runner/tests.py     2011-06-09 
23:51:03 UTC (rev 16351)
+++ django/trunk/tests/regressiontests/test_runner/tests.py     2011-06-10 
08:26:05 UTC (rev 16352)
@@ -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 @@
 
         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')

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to