all Python 3unittestfunction
unittest.installHandler()
Ctrl+C handling
Installs a Ctrl+C handler for tests: the first press lets the current test finish and ends the run with a report, the second one aborts at once.
Syntax
unittest.installHandler()
Without it, Ctrl+C in the middle of a long run raises KeyboardInterrupt and no summary is printed at all. The handler can only stop results registered through registerResult; TextTestRunner and unittest.main register theirs automatically. On the command line the -c (--catch) flag does the same, in unittest.main it is catchbreak=True.
Examples
import os
import signal
import unittest
class Tests(unittest.TestCase):
def test_1_press_ctrl_c(self):
os.kill(os.getpid(), signal.SIGINT) # имитация Ctrl+C
def test_2_never_runs(self):
pass
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
unittest.TestLoader().loadTestsFromTestCase(Tests).run(result)
print(result.testsRun, result.shouldStop) # → 1 True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy