Ticket #12364: ticket_12364.patch

File ticket_12364.patch, 3.1 KB (added by Randy Barlow, 9 years ago)

This patch adds the ability to quit on a second keyboard interrupt.

  • django/test/simple.py

     1import signal
    12import unittest
    24from django.conf import settings
    35from django.db.models import get_app, get_apps
    46from django.test import _doctest as doctest
    1517    def __init__(self, verbosity=0, failfast=False, **kwargs):
    1618        super(DjangoTestRunner, self).__init__(verbosity=verbosity, **kwargs)
    1719        self.failfast = failfast
     20        # We'll assume that the user hasn't yet typed CTRL-C :)
     21        self._keyboard_interrupt_intercepted = False
     23    def run(self, *args, **kwargs):
     24        """
     25        We override the run method, for the purposes of registering our custom signal handler for the KEYBOARD INTERRUPT.
     26        """
     27        # Let's remember the old keyboard interrupt, so we can replace it after run
     28        self._default_keyboard_interrupt_handler = signal.signal(signal.SIGINT, self._keyboard_interrupt_handler)
     29        result = super(DjangoTestRunner, self).run(*args, **kwargs)
     30        # Replace the keyboard interrupt handler
     31        signal.signal(signal.SIGINT, self._default_keyboard_interrupt_handler)
     32        return result
     34    def _keyboard_interrupt_handler(self, signal_number, stack_frame):
     35        """
     36        This method will be called when a user types CTRL-C, once it is registered as a handler for CTRL-C.
     37        """
     38        self._keyboard_interrupt_intercepted = True
     39        # Let's set the interrupt handler back to the default handler, so that if the user presses CTRL-C again, they can kill the test
     40        # suite.
     41        signal.signal(signal.SIGINT, self._default_keyboard_interrupt_handler)
    1943    def _makeResult(self):
    2044        result = super(DjangoTestRunner, self)._makeResult()
    2347        def stoptest_override(func):
    2448            def stoptest(test):
    25                 if failfast and not result.wasSuccessful():
     49                # If we were set to failfast and the unit test failed, or if the user has typed CTRL-C, let's report and quit
     50                if (failfast and not result.wasSuccessful()) or self._keyboard_interrupt_intercepted:
    2651                    result.stop()
    2752                func(test)
    2853            return stoptest
  • docs/ref/django-admin.txt

    694694.. django-admin:: test
    696696Runs tests for all installed models. See :ref:`topics-testing` for more
    697 information.
     697information. If you send a keyboard interrupt (CTRL-C) to the running test
     698suite, the currently running unit test will complete, and the test suite will
     699then halt, reporting to you any errors you've come across so far. This is very
     700useful if you have forgotten to pass the --failfast flag. If you don't wish to
     701wait for the current unit test to complete for whatever reason, you can send an
     702additional keyboard interrupt to stop it immediately.
Back to Top