unittest signal using semaphores

unittest version 3.2 updates the semaphore signal function for the end of the elegant encountered Ctrl C + keyboard interrupt.
When the user presses Ctrl + C, such as current or immediately after their use-case execution stops running, and outputs the result, if the user presses Ctrl + C again will immediately be thrown KeyboardInterrupt exception and stop.

Enable singal method has the following three functions:
exemplary embodiment test_demo.py use as follows:

import unittest

class TestDemo(unittest.TestCase):
    def test_a(self):
        pass

    def test_b(self):
        time.sleep(10)

    def test_c(self):
        time.sleep(10)
        pass

Run command line parameters when combined with -c or --catch

python -m unittest -vvv -c test_demo

Now press Ctrl + C, will immediately stop running, and outputs the result:

test_a (test_demo1.TestDemo) ... ok
test_b (test_demo1.TestDemo) ... ^Cok

----------------------------------------------------------------------
Ran 2 tests in 1.106s

OK

Parameter catchbreak = True () in unittest.main

In the script test_demo.py added:

if __name__ == '__main__':
    unittest.main(verbosity=2, catchbreak=True)

Use the python command on the command line to run the script:

python test_demo.py

Press Ctrl + C, this time to run the program does not stop immediately, but other cases after the implementation of the use , and then stopped, the output results are as follows:

test_a (__main__.TestDemo) ... ok
test_b (__main__.TestDemo) ... ^Cok

----------------------------------------------------------------------
Ran 2 tests in 30.005s

Using the signal in the suite

New run.py, code as follows:

import unittest
import test_demo

# 启用信号量功能
result = unittest.TestResult()
unittest.installHandler()
unittest.registerResult(result)

suite = unittest.defaultTestLoader.loadTestsFromModule(test_demo)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)

Use the command line to run python run.py:

python run.py

Press Ctrl + C, after waiting a second embodiment will be stopped execution, the results of a result supra. Press Ctrl + C again will stop immediately, output is as follows:

test_a (test_demo1.TestDemo) ... ok
test_b (test_demo1.TestDemo) ... ^C^CTraceback (most recent call last):
  File "run.py", line 11, in <module>
    runner.run(suite)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/runner.py", line 176, in run
    test(result)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 122, in run
    test(result)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 84, in __call__
    return self.run(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 122, in run
    test(result)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 653, in __call__
    return self.run(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/Users/apple/Documents/Projects/Secoo/rpa/outputs/test_demo1.py", line 12, in test_b
    time.sleep(30)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/signals.py", line 36, in __call__
    self.default_handler(signum, frame)
KeyboardInterrupt

Guess you like

Origin www.cnblogs.com/superhin/p/12607812.html
Recommended