Ticket #16185: 16185_15675.diff
File 16185_15675.diff, 9.3 KB (added by , 13 years ago) |
---|
-
AUTHORS
535 535 Cheng Zhang 536 536 Zlatko Mašek <zlatko.masek@gmail.com> 537 537 Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about> 538 Mikołaj Siedlarek <mikolaj.siedlarek@gmail.com> 539 Dmitry Jemerov <intelliyole@gmail.com> 538 540 539 541 A big THANK YOU goes to: 540 542 -
docs/topics/testing.txt
1744 1744 write your own test runner, ensure accept and handle the ``**kwargs`` 1745 1745 parameter. 1746 1746 1747 .. versionadded:: 1.4 1748 1749 Your test runner may define some command-line options which will be fed 1750 into the ``OptionParser`` of ``test`` management command. If you need such 1751 functionality, see the ``option_list`` attribute. Options you declare 1752 will be provided to your test runner's constructor as additional keyword 1753 arguments. 1754 1755 Attributes 1756 ~~~~~~~~~~ 1757 1758 .. versionadded:: 1.4 1759 1760 .. attribute:: DjangoTestSuiteRunner.option_list 1761 1762 This is the tuple of ``optparse`` options which will be fed into the management 1763 command's ``OptionParser`` for parsing arguments. 1764 1765 Methods 1766 ~~~~~~~ 1767 1747 1768 .. method:: DjangoTestSuiteRunner.run_tests(test_labels, extra_tests=None, **kwargs) 1748 1769 1749 1770 Run the test suite. -
docs/ref/django-admin.txt
964 964 Use the :djadminopt:`--failfast` option to stop running tests and report the failure 965 965 immediately after a test fails. 966 966 967 .. versionadded:: 1.4 968 .. django-admin-option:: --testrunner 969 970 The :djandminopt:`--testrunner` option can be used to override the test runner class 971 specified by the :setting:`TEST_RUNNER` setting. 972 967 973 testserver <fixture fixture ...> 968 974 -------------------------------- 969 975 -
django/test/utils.py
118 118 warnings.filters = state[:] 119 119 120 120 121 def get_runner(settings): 122 test_path = settings.TEST_RUNNER.split('.') 121 def get_runner(settings, test_runner_class=None): 122 if not test_runner_class: 123 test_runner_class = settings.TEST_RUNNER 124 125 test_path = test_runner_class.split('.') 123 126 # Allow for Python 2.5 relative paths 124 127 if len(test_path) > 1: 125 128 test_module_name = '.'.join(test_path[:-1]) -
django/core/management/commands/test.py
1 from django.conf import settings 1 2 from django.core.management.base import BaseCommand 2 from optparse import make_option 3 from optparse import make_option, OptionParser 3 4 import sys 5 from django.test.utils import get_runner 4 6 5 7 class Command(BaseCommand): 6 8 option_list = BaseCommand.option_list + ( 7 9 make_option('--noinput', action='store_false', dest='interactive', default=True, 8 10 help='Tells Django to NOT prompt the user for input of any kind.'), 9 11 make_option('--failfast', action='store_true', dest='failfast', default=False, 10 help='Tells Django to stop running the test suite after first failed test.') 12 help='Tells Django to stop running the test suite after first failed test.'), 13 make_option('--testrunner', action='store', dest='testrunner', 14 help='Tells Django to use specified test runner class instead of the one '+ 15 'specified by the TEST_RUNNER setting.') 11 16 ) 12 17 help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' 13 18 args = '[appname ...]' 14 19 15 20 requires_model_validation = False 16 21 22 def run_from_argv(self, argv): 23 """ 24 Pre-parse the command line to extract the value of the --testrunner 25 option. This allows a test runner to define additional command line 26 arguments. 27 """ 28 self.test_runner = None 29 option = '--testrunner=' 30 for arg in argv[2:]: 31 if arg.startswith(option): 32 self.test_runner = arg[len(option):] 33 break 34 super(Command, self).run_from_argv(argv) 35 36 def create_parser(self, prog_name, subcommand): 37 test_runner_class = get_runner(settings, self.test_runner) 38 options = self.option_list + getattr(test_runner_class, 'option_list', ()) 39 return OptionParser(prog=prog_name, 40 usage=self.usage(subcommand), 41 version=self.get_version(), 42 option_list=options) 43 17 44 def handle(self, *test_labels, **options): 18 45 from django.conf import settings 19 46 from django.test.utils import get_runner 20 47 21 verbosity = int(options.get('verbosity', 1))22 interactive = options.get('interactive', True)23 failfast = options.get('failfast', False)24 TestRunner = get_runner(settings)48 TestRunner = get_runner(settings, options.get('testrunner')) 49 options['verbosity'] = int(options.get('verbosity', 1)) 50 options.setdefault('interactive', True) 51 options.setdefault('failfast', False) 25 52 26 test_runner = TestRunner( verbosity=verbosity, interactive=interactive, failfast=failfast)53 test_runner = TestRunner(**options) 27 54 failures = test_runner.run_tests(test_labels) 28 55 29 56 if failures: -
tests/regressiontests/test_runner/tests.py
2 2 Tests for django test runner 3 3 """ 4 4 import StringIO 5 from optparse import make_option 5 6 import warnings 6 7 7 8 from django.core.exceptions import ImproperlyConfigured 9 from django.core.management import call_command 8 10 from django.test import simple 9 11 from django.test.utils import get_warnings_state, restore_warnings_state 10 12 from django.utils import unittest 13 from regressiontests.admin_scripts.tests import AdminScriptTestCase 11 14 12 15 13 16 class DjangoTestRunnerTests(unittest.TestCase): … … 128 131 129 132 self.assertRaises(ImproperlyConfigured, simple.dependency_ordered, raw, dependencies=dependencies) 130 133 134 135 class MockTestRunner(object): 136 invoked = False 137 138 def __init__(self, *args, **kwargs): 139 pass 140 141 def run_tests(self, test_labels, extra_tests=None, **kwargs): 142 MockTestRunner.invoked = True 143 144 145 class ManageCommandTests(unittest.TestCase): 146 147 def test_custom_test_runner(self): 148 call_command('test', 'sites', 149 testrunner='regressiontests.test_runner.tests.MockTestRunner') 150 self.assertTrue(MockTestRunner.invoked, 151 "The custom test runner has not been invoked") 152 153 154 class CustomOptionsTestRunner(simple.DjangoTestSuiteRunner): 155 option_list = ( 156 make_option('--option_a','-a', action='store', dest='option_a', default='1'), 157 make_option('--option_b','-b', action='store', dest='option_b', default='2'), 158 make_option('--option_c','-c', action='store', dest='option_c', default='3'), 159 ) 160 161 def __init__(self, verbosity=1, interactive=True, failfast=True, option_a=None, option_b=None, option_c=None, **kwargs): 162 super(CustomOptionsTestRunner, self).__init__(verbosity=verbosity, interactive=interactive, 163 failfast=failfast) 164 self.option_a = option_a 165 self.option_b = option_b 166 self.option_c = option_c 167 168 def run_tests(self, test_labels, extra_tests=None, **kwargs): 169 print "%s:%s:%s" % (self.option_a, self.option_b, self.option_c) 170 171 172 class CustomTestRunnerOptionsTests(AdminScriptTestCase): 173 174 def setUp(self): 175 settings = { 176 'TEST_RUNNER': '\'regressiontests.test_runner.tests.CustomOptionsTestRunner\'', 177 } 178 self.write_settings('settings.py', sdict=settings) 179 180 def tearDown(self): 181 self.remove_settings('settings.py') 182 183 def test_default_options(self): 184 args = ['test', '--settings=settings'] 185 out, err = self.run_django_admin(args) 186 self.assertNoOutput(err) 187 self.assertOutput(out, '1:2:3') 188 189 def test_default_and_given_options(self): 190 args = ['test', '--settings=settings', '--option_b=foo'] 191 out, err = self.run_django_admin(args) 192 self.assertNoOutput(err) 193 self.assertOutput(out, '1:foo:3') 194 195 def test_option_name_and_value_separated(self): 196 args = ['test', '--settings=settings', '--option_b', 'foo'] 197 out, err = self.run_django_admin(args) 198 self.assertNoOutput(err) 199 self.assertOutput(out, '1:foo:3') 200 201 def test_all_options_given(self): 202 args = ['test', '--settings=settings', '--option_a=bar', '--option_b=foo', '--option_c=31337'] 203 out, err = self.run_django_admin(args) 204 self.assertNoOutput(err) 205 self.assertOutput(out, 'bar:foo:31337')