Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having program exit with ctrl c

I have a script which runs a number of unit tests, but when I hit control c it just exits the particular unit test, not the whole script. Is there any way I can achieve this? This is my code which actually performs the execution of the unit tests.

    #bitshift right to obtain correct return value, execution of each test.
    returnValue = os.system(path) >> 8
    #running total of failures in the program.
    failures += returnValue

This is how I call this function and exit the program.

failures = execTests(path, testList)
#exit program with returncode as number of failures
sys.exit(failures)
like image 836
Alan Gordon Avatar asked Feb 02 '26 18:02

Alan Gordon


1 Answers

In fact, this feature is already present in the API. Coming from the The Python Standard Library,25.3. unittest — Unit testing framework here is the relevant excerpt:

-c, --catch

Control-C during the test run waits for the current test to end and then reports all the results so far. A second Control-C raises the normal KeyboardInterrupt exception.

See Signal Handling for the functions that provide this functionality.

like image 160
perror Avatar answered Feb 05 '26 07:02

perror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!