mirror of https://github.com/django/django.git
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łaj Siedlarek for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16352 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
b56ef75088
commit
046ffa483e
6
AUTHORS
6
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 <intelliyole@gmail.com>
|
||||
jhenry <jhenry@theonion.com>
|
||||
john@calixto.net
|
||||
Zak Johnson <zakj@nox.cx>
|
||||
|
@ -338,6 +339,7 @@ answer newbie questions, and generally made Django that much better:
|
|||
mark@junklight.com
|
||||
Orestis Markou <orestis@orestis.gr>
|
||||
Takashi Matsuo <matsuo.takashi@gmail.com>
|
||||
Zlatko Mašek <zlatko.masek@gmail.com>
|
||||
Yasushi Masuda <whosaysni@gmail.com>
|
||||
mattycakes@gmail.com
|
||||
Glenn Maynard <glenn@zewt.org>
|
||||
|
@ -369,6 +371,7 @@ answer newbie questions, and generally made Django that much better:
|
|||
Gopal Narayanan <gopastro@gmail.com>
|
||||
Fraser Nevett <mail@nevett.org>
|
||||
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 <fonzzo.django@gmail.com>
|
||||
Neal Norwitz <nnorwitz@google.com>
|
||||
|
@ -443,6 +446,7 @@ answer newbie questions, and generally made Django that much better:
|
|||
Pete Shinners <pete@shinners.org>
|
||||
Leo Shklovskii
|
||||
jason.sidabras@gmail.com
|
||||
Mikołaj Siedlarek <mikolaj.siedlarek@gmail.com>
|
||||
Brenton Simpson <http://theillustratedlife.com>
|
||||
Jozko Skrablin <jozko.skrablin@gmail.com>
|
||||
Ben Slavin <benjamin.slavin@gmail.com>
|
||||
|
@ -533,8 +537,6 @@ answer newbie questions, and generally made Django that much better:
|
|||
Gasper Zejn <zejn@kiberpipa.org>
|
||||
Jarek Zgoda <jarek.zgoda@gmail.com>
|
||||
Cheng Zhang
|
||||
Zlatko Mašek <zlatko.masek@gmail.com>
|
||||
Ryan Niemeyer <https://profiles.google.com/ryan.niemeyer/about>
|
||||
|
||||
A big THANK YOU goes to:
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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])
|
||||
|
|
|
@ -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 <fixture fixture ...>
|
||||
--------------------------------
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue