Ticket #15675: ticket15675-3.patch
File ticket15675-3.patch, 6.3 KB (added by , 14 years ago) |
---|
-
django/core/management/commands/test.py
diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py index 1431f00..c400a78 100644
a b 1 1 from django.core.management.base import BaseCommand 2 from django.test.utils import get_runner 3 from django.conf import settings 2 4 from optparse import make_option 3 5 import sys 4 6 7 TestRunner = get_runner(settings) 8 5 9 class Command(BaseCommand): 6 10 option_list = BaseCommand.option_list + ( 7 11 make_option('--noinput', action='store_false', dest='interactive', default=True, 8 12 help='Tells Django to NOT prompt the user for input of any kind.'), 9 13 make_option('--failfast', action='store_true', dest='failfast', default=False, 10 14 help='Tells Django to stop running the test suite after first failed test.') 11 ) 15 ) + getattr(TestRunner, 'option_list', ()) 12 16 help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' 13 17 args = '[appname ...]' 14 18 15 19 requires_model_validation = False 16 20 17 21 def handle(self, *test_labels, **options): 18 from django.conf import settings 19 from django.test.utils import get_runner 20 21 verbosity = int(options.get('verbosity', 1)) 22 interactive = options.get('interactive', True) 23 failfast = options.get('failfast', False) 24 TestRunner = get_runner(settings) 22 options['verbosity'] = int(options.get('verbosity', 1)) 23 options.setdefault('interactive', True) 24 options.setdefault('failfast', False) 25 25 26 26 if hasattr(TestRunner, 'func_name'): 27 27 # Pre 1.2 test runners were just functions, … … class Command(BaseCommand): 31 31 'Function-based test runners are deprecated. Test runners should be classes with a run_tests() method.', 32 32 DeprecationWarning 33 33 ) 34 failures = TestRunner(test_labels, verbosity=verbosity, interactive=interactive)34 failures = TestRunner(test_labels, **options) 35 35 else: 36 test_runner = TestRunner( verbosity=verbosity, interactive=interactive, failfast=failfast)36 test_runner = TestRunner(**options) 37 37 failures = test_runner.run_tests(test_labels) 38 38 39 39 if failures: -
docs/topics/testing.txt
diff --git a/docs/topics/testing.txt b/docs/topics/testing.txt index 4176857..9a2b9d4 100644
a b set up, execute and tear down the test suite. 1645 1645 write your own test runner, ensure accept and handle the ``**kwargs`` 1646 1646 parameter. 1647 1647 1648 .. versionadded:: development 1649 1650 Your test runner may define some command-line options which will be fed 1651 into ``test`` management command's ``OptionParser``. If you need such 1652 functionality, see the ``option_list`` attribute. Options you declare 1653 will be provided to your test runner's constructor as additional keyword 1654 arguments. 1655 1656 Attributes 1657 ~~~~~~~~~~ 1658 1659 .. versionadded:: development 1660 1661 .. attribute:: DjangoTestSuiteRunner.option_list 1662 1663 This is the tuple of ``optparse`` options which will be fed into the management 1664 command's ``OptionParser`` for parsing arguments. 1665 1666 Methods 1667 ~~~~~~~ 1668 1648 1669 .. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs) 1649 1670 1650 1671 Run the test suite. -
tests/regressiontests/test_runner/tests.py
diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py index dfdd574..bf4e4a7 100644
a b 2 2 Tests for django test runner 3 3 """ 4 4 import StringIO 5 from optparse import make_option 5 6 6 7 from django.core.exceptions import ImproperlyConfigured 7 8 from django.test import simple 8 9 from django.utils import unittest 9 10 11 from regressiontests.admin_scripts.tests import AdminScriptTestCase 12 10 13 class DjangoTestRunnerTests(unittest.TestCase): 11 14 12 15 def test_failfast(self): … … class DependencyOrderingTests(unittest.TestCase): 118 121 119 122 self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies) 120 123 124 class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner): 125 option_list = ( 126 make_option('--option_a','-a', action='store', dest='option_a', default='1'), 127 make_option('--option_b','-b', action='store', dest='option_b', default='2'), 128 make_option('--option_c','-c', action='store', dest='option_c', default='3'), 129 ) 130 131 def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs): 132 super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive, 133 failfast=failfast) 134 self.option_a = option_a 135 self.option_b = option_b 136 self.option_c = option_c 137 138 def run_tests(self, test_labels, extra_tests=None, **kwargs): 139 print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c) 140 141 class CustomTestRunnerOptionsTests(AdminScriptTestCase): 142 143 def setUp(self): 144 settings = { 145 'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'', 146 } 147 self.write_settings('settings.py', sdict=settings) 148 149 def tearDown(self): 150 self.remove_settings('settings.py') 151 152 def test_default_options(self): 153 args = ['test', '--settings=settings'] 154 out, err = self.run_django_admin(args) 155 self.assertNoOutput(err) 156 self.assertOutput(out, '1:2:3') 157 158 def test_default_and_given_options(self): 159 args = ['test', '--settings=settings', '--option_b=foo'] 160 out, err = self.run_django_admin(args) 161 self.assertNoOutput(err) 162 self.assertOutput(out, '1:foo:3') 163 164 def test_option_name_and_value_separated(self): 165 args = ['test', '--settings=settings', '--option_b', 'foo'] 166 out, err = self.run_django_admin(args) 167 self.assertNoOutput(err) 168 self.assertOutput(out, '1:foo:3') 169 170 def test_all_options_given(self): 171 args = ['test', '--settings=settings', '--option_a=bar', '--option_b=foo', '--option_c=31337'] 172 out, err = self.run_django_admin(args) 173 self.assertNoOutput(err) 174 self.assertOutput(out, 'bar:foo:31337') 175