Author: kmtracey
Date: 2009-12-13 10:24:36 -0600 (Sun, 13 Dec 2009)
New Revision: 11843

Modified:
   django/trunk/django/core/management/commands/test.py
   django/trunk/django/test/simple.py
   django/trunk/docs/ref/django-admin.txt
   django/trunk/tests/runtests.py
Log:
Fixed #11613: Added a failfast option for test running.  Thanks jukvalim and 
Randy Barlow.


Modified: django/trunk/django/core/management/commands/test.py
===================================================================
--- django/trunk/django/core/management/commands/test.py        2009-12-13 
15:19:21 UTC (rev 11842)
+++ django/trunk/django/core/management/commands/test.py        2009-12-13 
16:24:36 UTC (rev 11843)
@@ -6,6 +6,8 @@
     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 @@
     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)

Modified: django/trunk/django/test/simple.py
===================================================================
--- django/trunk/django/test/simple.py  2009-12-13 15:19:21 UTC (rev 11842)
+++ django/trunk/django/test/simple.py  2009-12-13 16:24:36 UTC (rev 11843)
@@ -10,6 +10,26 @@
 
 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 @@
         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 @@
     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()

Modified: django/trunk/docs/ref/django-admin.txt
===================================================================
--- django/trunk/docs/ref/django-admin.txt      2009-12-13 15:19:21 UTC (rev 
11842)
+++ django/trunk/docs/ref/django-admin.txt      2009-12-13 16:24:36 UTC (rev 
11843)
@@ -696,6 +696,12 @@
 Runs tests for all installed models. See :ref:`topics-testing` for more
 information.
 
+--failfast
+~~~~~~~~
+
+Use the ``--failfast`` option to stop running tests and report the failure 
+immediately after a test fails.
+
 testserver <fixture fixture ...>
 --------------------------------
 

Modified: django/trunk/tests/runtests.py
===================================================================
--- django/trunk/tests/runtests.py      2009-12-13 15:19:21 UTC (rev 11842)
+++ django/trunk/tests/runtests.py      2009-12-13 16:24:36 UTC (rev 11843)
@@ -86,7 +86,7 @@
         self.assert_(not unexpected, "Unexpected Errors: " + 
'\n'.join(unexpected))
         self.assert_(not missing, "Missing Errors: " + '\n'.join(missing))
 
-def django_tests(verbosity, interactive, test_labels):
+def django_tests(verbosity, interactive, failfast, test_labels):
     from django.conf import settings
 
     old_installed_apps = settings.INSTALLED_APPS
@@ -160,7 +160,8 @@
         settings.TEST_RUNNER = 'django.test.simple.run_tests'
     test_runner = get_runner(settings)
 
-    failures = test_runner(test_labels, verbosity=verbosity, 
interactive=interactive, extra_tests=extra_tests)
+    failures = test_runner(test_labels, verbosity=verbosity, 
interactive=interactive, failfast=failfast,
+                           extra_tests=extra_tests)
     if failures:
         sys.exit(failures)
 
@@ -182,6 +183,8 @@
         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('--failfast', action='store_true', dest='failfast', 
default=False,
+        help='Tells Django to stop running the test suite after first failed 
test.')
     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()
@@ -190,4 +193,4 @@
     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), options.interactive, args)
+    django_tests(int(options.verbosity), options.interactive, 
options.failfast, args)

--

You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.


Reply via email to