Skip to content

Commit 1b7fe09

Browse files
committed
Fixed #3771 -- Modified the test runner to observe the --noinput argument controlling script interactivity. This means that test scripts can now be put in a buildbot environment. This is a backwards incompatible change for anyone that has written a custom test runner. Thanks for the suggestion, moof@metamoof.net.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5752 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 3043816 commit 1b7fe09

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

django/core/management.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ def runfcgi(args):
13311331
runfastcgi(args)
13321332
runfcgi.args = '[various KEY=val options, use `runfcgi help` for help]'
13331333

1334-
def test(app_labels, verbosity=1):
1334+
def test(app_labels, verbosity=1, interactive=True):
13351335
"Runs the test suite for the specified applications"
13361336
from django.conf import settings
13371337
from django.db.models import get_app, get_apps
@@ -1350,12 +1350,12 @@ def test(app_labels, verbosity=1):
13501350
test_module = __import__(test_module_name, {}, {}, test_path[-1])
13511351
test_runner = getattr(test_module, test_path[-1])
13521352

1353-
failures = test_runner(app_list, verbosity)
1353+
failures = test_runner(app_list, verbosity=verbosity, interactive=interactive)
13541354
if failures:
13551355
sys.exit(failures)
13561356

13571357
test.help_doc = 'Runs the test suite for the specified applications, or the entire site if no apps are specified'
1358-
test.args = '[--verbosity] ' + APP_ARGS
1358+
test.args = '[--verbosity] [--noinput]' + APP_ARGS
13591359

13601360
def load_data(fixture_labels, verbosity=1):
13611361
"Installs the provided fixture file(s) as data in the database."
@@ -1631,7 +1631,12 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
16311631
action_mapping[action](args[1])
16321632
except IndexError:
16331633
parser.print_usage_and_exit()
1634-
elif action in ('test', 'loaddata'):
1634+
elif action == 'test':
1635+
try:
1636+
action_mapping[action](args[1:], int(options.verbosity), options.interactive)
1637+
except IndexError:
1638+
parser.print_usage_and_exit()
1639+
elif action == 'loaddata':
16351640
try:
16361641
action_mapping[action](args[1:], int(options.verbosity))
16371642
except IndexError:

django/test/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def build_suite(app_module):
6969

7070
return suite
7171

72-
def run_tests(module_list, verbosity=1, extra_tests=[]):
72+
def run_tests(module_list, verbosity=1, interactive=True, extra_tests=[]):
7373
"""
7474
Run the unit tests for all the modules in the provided list.
7575
This testrunner will search each of the modules in the provided list,
@@ -91,7 +91,7 @@ def run_tests(module_list, verbosity=1, extra_tests=[]):
9191
suite.addTest(test)
9292

9393
old_name = settings.DATABASE_NAME
94-
create_test_db(verbosity)
94+
create_test_db(verbosity, autoclobber=not interactive)
9595
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
9696
destroy_test_db(old_name, verbosity)
9797

docs/testing.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,17 +662,23 @@ framework that can be executed from Python code.
662662
Defining a test runner
663663
----------------------
664664
By convention, a test runner should be called ``run_tests``; however, you
665-
can call it anything you want. The only requirement is that it accept two
665+
can call it anything you want. The only requirement is that it accept three
666666
arguments:
667667

668-
``run_tests(module_list, verbosity=1)``
668+
``run_tests(module_list, verbosity=1, interactive=True)``
669669
The module list is the list of Python modules that contain the models to be
670670
tested. This is the same format returned by ``django.db.models.get_apps()``
671671

672672
Verbosity determines the amount of notification and debug information that
673673
will be printed to the console; ``0`` is no output, ``1`` is normal output,
674674
and ``2`` is verbose output.
675675

676+
**New in Django development version** If ``interactive`` is ``True``, the
677+
test suite may ask the user for instructions when the test suite is
678+
executed. An example of this behavior would be asking for permission to
679+
delete an existing test database. If ``interactive`` is ``False, the
680+
test suite must be able to run without any manual intervention.
681+
676682
This method should return the number of tests that failed.
677683

678684
Testing utilities

tests/runtests.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def runTest(self):
7373
self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
7474
self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))
7575

76-
def django_tests(verbosity, tests_to_run):
76+
def django_tests(verbosity, interactive, tests_to_run):
7777
from django.conf import settings
7878

7979
old_installed_apps = settings.INSTALLED_APPS
@@ -130,7 +130,7 @@ def django_tests(verbosity, tests_to_run):
130130

131131
# Run the test suite, including the extra validation tests.
132132
from django.test.simple import run_tests
133-
failures = run_tests(test_models, verbosity, extra_tests=extra_tests)
133+
failures = run_tests(test_models, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests)
134134
if failures:
135135
sys.exit(failures)
136136

@@ -149,6 +149,8 @@ def django_tests(verbosity, tests_to_run):
149149
parser.add_option('-v','--verbosity', action='store', dest='verbosity', default='0',
150150
type='choice', choices=['0', '1', '2'],
151151
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
152+
parser.add_option('--noinput', action='store_false', dest='interactive', default=True,
153+
help='Tells Django to NOT prompt the user for input of any kind.')
152154
parser.add_option('--settings',
153155
help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.')
154156
options, args = parser.parse_args()
@@ -157,4 +159,4 @@ def django_tests(verbosity, tests_to_run):
157159
elif "DJANGO_SETTINGS_MODULE" not in os.environ:
158160
parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
159161
"Set it or use --settings.")
160-
django_tests(int(options.verbosity), args)
162+
django_tests(int(options.verbosity), options.interactive, args)

0 commit comments

Comments
 (0)