Python Glossary Русский

all Python 3unittestfunction

unittest.registerResult()

Ctrl+C handling

Registers a TestResult object with the Ctrl+C handler so that an interrupt stops this run instead of killing the process.

Syntax

unittest.registerResult(result)

You only need it when running tests from your own code via suite.run(result): TextTestRunner registers its result by itself. Without installHandler() registering changes nothing, and Ctrl+C still raises KeyboardInterrupt. The module keeps a weak reference, so a registered result does not keep the object alive.

Examples

import os
import signal
import unittest
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
os.kill(os.getpid(), signal.SIGINT)  # имитация Ctrl+C
print(result.shouldStop)  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy