From 92eec3ef9ae3eed18cef4cb7e87a85df4f06d3f0 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sun, 13 Dec 2009 16:24:36 +0000 Subject: Fixed #11613: Added a failfast option for test running. Thanks jukvalim and Randy Barlow. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11843 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/management/commands/test.py | 13 +++++++++++-- django/test/simple.py | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'django') diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py index 8ebf3daea6..4fd6ba0c8d 100644 --- a/django/core/management/commands/test.py +++ b/django/core/management/commands/test.py @@ -6,6 +6,8 @@ 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 = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' args = '[appname ...]' @@ -15,11 +17,18 @@ class Command(BaseCommand): 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) test_runner = get_runner(settings) - failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive) + # Some custom test runners won't accept the failfast flag, so let's make sure they accept it before passing it to them + if 'failfast' in test_runner.func_code.co_varnames: + failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, + failfast=failfast) + else: + failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive) + if failures: sys.exit(failures) diff --git a/django/test/simple.py b/django/test/simple.py index f3c48bae33..c1f915b8cd 100644 --- a/django/test/simple.py +++ b/django/test/simple.py @@ -10,6 +10,26 @@ TEST_MODULE = 'tests' doctestOutputChecker = OutputChecker() +class DjangoTestRunner(unittest.TextTestRunner): + + def __init__(self, verbosity=0, failfast=False, **kwargs): + super(DjangoTestRunner, self).__init__(verbosity=verbosity, **kwargs) + self.failfast = failfast + + def _makeResult(self): + result = super(DjangoTestRunner, self)._makeResult() + failfast = self.failfast + + def stoptest_override(func): + def stoptest(test): + if failfast and not result.wasSuccessful(): + result.stop() + func(test) + return stoptest + + setattr(result, 'stopTest', stoptest_override(result.stopTest)) + return result + def get_tests(app_module): try: app_path = app_module.__name__.split('.')[:-1] @@ -146,7 +166,7 @@ def reorder_suite(suite, classes): bins[0].addTests(bins[i+1]) return bins[0] -def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): +def run_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: @@ -189,7 +209,7 @@ def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) - result = unittest.TextTestRunner(verbosity=verbosity).run(suite) + result = DjangoTestRunner(verbosity=verbosity, failfast=failfast).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() -- cgit v1.3