Ticket #11613: failfast_1.diff
File failfast_1.diff, 7.3 KB (added by , 15 years ago) |
---|
-
django/test/simple.py
10 10 11 11 doctestOutputChecker = OutputChecker() 12 12 13 class DjangoTestRunner(unittest.TextTestRunner): 14 15 def __init__(self, verbosity=0, failfast=False, **kwargs): 16 super(DjangoTestRunner, self).__init__(verbosity=verbosity, **kwargs) 17 self.failfast = failfast 18 19 def _makeResult(self): 20 result = super(DjangoTestRunner, self)._makeResult() 21 failfast = self.failfast 22 23 def stoptest_override(func): 24 def stoptest(test): 25 if failfast and not result.wasSuccessful(): 26 result.stop() 27 func(test) 28 return stoptest 29 30 setattr(result, 'stopTest', stoptest_override(result.stopTest)) 31 return result 32 13 33 def get_tests(app_module): 14 34 try: 15 35 app_path = app_module.__name__.split('.')[:-1] … … 146 166 bins[0].addTests(bins[i+1]) 147 167 return bins[0] 148 168 149 def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):169 def run_tests(test_labels, verbosity=1, interactive=True, failfast=False, extra_tests=[]): 150 170 """ 151 171 Run the unit tests for all the test labels in the provided list. 152 172 Labels must be of the form: … … 189 209 old_name = settings.DATABASE_NAME 190 210 from django.db import connection 191 211 connection.creation.create_test_db(verbosity, autoclobber=not interactive) 192 result = unittest.TextTestRunner(verbosity=verbosity).run(suite)212 result = DjangoTestRunner(verbosity=verbosity, failfast=failfast).run(suite) 193 213 connection.creation.destroy_test_db(old_name, verbosity) 194 214 195 215 teardown_test_environment() -
django/core/management/commands/test.py
6 6 option_list = BaseCommand.option_list + ( 7 7 make_option('--noinput', action='store_false', dest='interactive', default=True, 8 8 help='Tells Django to NOT prompt the user for input of any kind.'), 9 make_option('--failfast', action='store_true', dest='failfast', default=False, 10 help='Tells Django to stop running the test suite after first failed test.') 9 11 ) 10 12 help = 'Runs the test suite for the specified applications, or the entire site if no apps are specified.' 11 13 args = '[appname ...]' … … 15 17 def handle(self, *test_labels, **options): 16 18 from django.conf import settings 17 19 from django.test.utils import get_runner 18 20 19 21 verbosity = int(options.get('verbosity', 1)) 20 22 interactive = options.get('interactive', True) 23 failfast = options.get('failfast', True) 21 24 test_runner = get_runner(settings) 22 25 23 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive) 26 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, 27 failfast=failfast) 24 28 if failures: 25 29 sys.exit(failures) -
tests/regressiontests/test_runner/tests.py
1 """ 2 Tests for django test runner 3 """ 4 import StringIO 5 import unittest 6 import django 7 from django.test import TestCase, TransactionTestCase, simple 8 9 class DjangoTestRunnerTests(TestCase): 10 11 def test_failfast(self): 12 class MockTestOne(TransactionTestCase): 13 def runTest(self): 14 assert False 15 class MockTestTwo(TransactionTestCase): 16 def runTest(self): 17 assert False 18 19 suite = unittest.TestSuite([MockTestOne(), MockTestTwo()]) 20 mock_stream = StringIO.StringIO() 21 dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream) 22 result = dtr.run(suite) 23 self.assertEqual(2, result.testsRun) 24 self.assertEqual(2, len(result.failures)) 25 26 dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream) 27 result = dtr.run(suite) 28 self.assertEqual(1, result.testsRun) 29 self.assertEqual(1, len(result.failures)) 30 No newline at end of file -
tests/runtests.py
85 85 self.assert_(not unexpected, "Unexpected Errors: " + '\n'.join(unexpected)) 86 86 self.assert_(not missing, "Missing Errors: " + '\n'.join(missing)) 87 87 88 def django_tests(verbosity, interactive, test_labels):88 def django_tests(verbosity, interactive, failfast, test_labels): 89 89 from django.conf import settings 90 90 91 91 old_installed_apps = settings.INSTALLED_APPS … … 158 158 settings.TEST_RUNNER = 'django.test.simple.run_tests' 159 159 test_runner = get_runner(settings) 160 160 161 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, extra_tests=extra_tests) 161 failures = test_runner(test_labels, verbosity=verbosity, interactive=interactive, failfast=failfast, 162 extra_tests=extra_tests) 162 163 if failures: 163 164 sys.exit(failures) 164 165 … … 180 181 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output') 181 182 parser.add_option('--noinput', action='store_false', dest='interactive', default=True, 182 183 help='Tells Django to NOT prompt the user for input of any kind.') 184 parser.add_option('--failfast', action='store_true', dest='failfast', default=False, 185 help='Tells Django to stop running the test suite after first failed test.') 183 186 parser.add_option('--settings', 184 187 help='Python path to settings module, e.g. "myproject.settings". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') 185 188 options, args = parser.parse_args() … … 188 191 elif "DJANGO_SETTINGS_MODULE" not in os.environ: 189 192 parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. " 190 193 "Set it or use --settings.") 191 django_tests(int(options.verbosity), options.interactive, args)194 django_tests(int(options.verbosity), options.interactive, options.failfast, args) -
docs/ref/django-admin.txt
689 689 "Are you sure?" confirmation messages. This is useful if ``django-admin.py`` 690 690 is being executed as an unattended, automated script. 691 691 692 --failfast 693 ~~~~~~~~ 694 695 Use the ``--failfast`` option to stop running tests and report the failure 696 immediately after a test fails. 697 692 698 testserver <fixture fixture ...> 693 699 -------------------------------- 694 700