Skip to content

Commit

Permalink
Fixed #3771 -- Modified the test runner to observe the --noinput argu…
Browse files Browse the repository at this point in the history
…ment 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
  • Loading branch information
freakboy3742 committed Jul 23, 2007
1 parent 3043816 commit 1b7fe09
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
13 changes: 9 additions & 4 deletions django/core/management.py
Expand Up @@ -1331,7 +1331,7 @@ def runfcgi(args):
runfastcgi(args)
runfcgi.args = '[various KEY=val options, use `runfcgi help` for help]'

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

failures = test_runner(app_list, verbosity)
failures = test_runner(app_list, verbosity=verbosity, interactive=interactive)
if failures:
sys.exit(failures)

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

def load_data(fixture_labels, verbosity=1):
"Installs the provided fixture file(s) as data in the database."
Expand Down Expand Up @@ -1631,7 +1631,12 @@ def execute_from_command_line(action_mapping=DEFAULT_ACTION_MAPPING, argv=None):
action_mapping[action](args[1])
except IndexError:
parser.print_usage_and_exit()
elif action in ('test', 'loaddata'):
elif action == 'test':
try:
action_mapping[action](args[1:], int(options.verbosity), options.interactive)
except IndexError:
parser.print_usage_and_exit()
elif action == 'loaddata':
try:
action_mapping[action](args[1:], int(options.verbosity))
except IndexError:
Expand Down
4 changes: 2 additions & 2 deletions django/test/simple.py
Expand Up @@ -69,7 +69,7 @@ def build_suite(app_module):

return suite

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

old_name = settings.DATABASE_NAME
create_test_db(verbosity)
create_test_db(verbosity, autoclobber=not interactive)
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
destroy_test_db(old_name, verbosity)

Expand Down
10 changes: 8 additions & 2 deletions docs/testing.txt
Expand Up @@ -662,17 +662,23 @@ framework that can be executed from Python code.
Defining a test runner
----------------------
By convention, a test runner should be called ``run_tests``; however, you
can call it anything you want. The only requirement is that it accept two
can call it anything you want. The only requirement is that it accept three
arguments:

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

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

**New in Django development version** If ``interactive`` is ``True``, the
test suite may ask the user for instructions when the test suite is
executed. An example of this behavior would be asking for permission to
delete an existing test database. If ``interactive`` is ``False, the
test suite must be able to run without any manual intervention.

This method should return the number of tests that failed.

Testing utilities
Expand Down
8 changes: 5 additions & 3 deletions tests/runtests.py
Expand Up @@ -73,7 +73,7 @@ def runTest(self):
self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected))
self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))

def django_tests(verbosity, tests_to_run):
def django_tests(verbosity, interactive, tests_to_run):
from django.conf import settings

old_installed_apps = settings.INSTALLED_APPS
Expand Down Expand Up @@ -130,7 +130,7 @@ def django_tests(verbosity, tests_to_run):

# Run the test suite, including the extra validation tests.
from django.test.simple import run_tests
failures = run_tests(test_models, verbosity, extra_tests=extra_tests)
failures = run_tests(test_models, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests)
if failures:
sys.exit(failures)

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

0 comments on commit 1b7fe09

Please sign in to comment.