You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
sys.exit() in a test causes a test run to die #54820
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/voidspace'closed_at=<Date2010-12-19.14:56:28.056>created_at=<Date2010-12-03.01:25:17.075>labels= ['easy', 'type-bug']
title='sys.exit() in a test causes a test run to die'updated_at=<Date2021-09-17.13:18:30.937>user='https://github.com/voidspace'
changed the title [-]sys.exit() in a test causes the run to stp[/-][+]sys.exit() in a test causes a test run to die[/+]on Dec 3, 2010
ysjray commented on Dec 10, 2010
ysjraymannequin
Mannequin
More actions
Agreed. I think the "except Exception" in TestCase.run() should be "except BaseException", since BaseException could catch Exception, SystemExit, GeneratorExit, KeyboardInterrupt. The KeyboardInterrupt should be caught first. The remaining three is exactly what is needed.
At the moment exception handling for setUp / tearDown / testMethod and cleanUp functions are all handled separately. They all have to call addError and as a result we have inconsistent handling of skips, expected failures (etc). There are separate issues for handling expected failures in setUp and skips in tearDown.
I'd like to fix all these issues by moving the exception handling into a single method and unifying the reporting of failure / error / expected failure / skip test. This will fix all these issues and nicely simplify the implementation.
ysjray commented on Dec 16, 2010
ysjraymannequin
Mannequin
More actions
I'd like to fix all these issues by moving the exception handling into a single method and unifying the reporting of failure / error / expected failure / skip test. This will fix all these issues and nicely simplify the implementation.
tl;dr - I think unittest should not prevent this causing the process to exit (but it should still fail the test and fail the test run as a whole), analgous to how KeyboardInterrupt is handled.
Allowing sys.exit() to end the test run was particularly a problem for testing command line tools, where improper patching / unexpected code paths would trigger a sys.exit.
If a test framework author wants a way to end the test run I'm happy to provide that (a custom exception to raise or a flag to turn off sys.exit handling), but I don't think having sys.exit kill test runs is best for test authors.
Activity
voidspace commentedon Dec 3, 2010
Reported by a unittest2 user.
A SystemExit (or GeneratorExit) will cause a test run to stop in 2.7 / 3.2. This would just be reported as an error in 2.6.
Above code works in Python 2.6 (the exception is caught by TestCase.run) but dies in 2.7 / 3.2
[-]sys.exit() in a test causes the run to stp[/-][+]sys.exit() in a test causes a test run to die[/+]ysjray commentedon Dec 10, 2010
Agreed. I think the "except Exception" in TestCase.run() should be "except BaseException", since BaseException could catch Exception, SystemExit, GeneratorExit, KeyboardInterrupt. The KeyboardInterrupt should be caught first. The remaining three is exactly what is needed.
Here is a patch I worked, with unittest.
merwok commentedon Dec 10, 2010
LGTM.
voidspace commentedon Dec 10, 2010
At the moment exception handling for setUp / tearDown / testMethod and cleanUp functions are all handled separately. They all have to call addError and as a result we have inconsistent handling of skips, expected failures (etc). There are separate issues for handling expected failures in setUp and skips in tearDown.
I'd like to fix all these issues by moving the exception handling into a single method and unifying the reporting of failure / error / expected failure / skip test. This will fix all these issues and nicely simplify the implementation.
ysjray commentedon Dec 16, 2010
That sounds good.
voidspace commentedon Dec 19, 2010
Committed to Python 2.7 in revision 87406.
voidspace commentedon Dec 19, 2010
Committed to py3k in revision 87390.
rbtcollins commentedon Nov 7, 2014
Hmm, so testtools went in a different direction here - the same unification stuff, but see testing-cabal/testtools@18bc574
tl;dr - I think unittest should not prevent this causing the process to exit (but it should still fail the test and fail the test run as a whole), analgous to how KeyboardInterrupt is handled.
voidspace commentedon Nov 7, 2014
Allowing sys.exit() to end the test run was particularly a problem for testing command line tools, where improper patching / unexpected code paths would trigger a sys.exit.
If a test framework author wants a way to end the test run I'm happy to provide that (a custom exception to raise or a flag to turn off sys.exit handling), but I don't think having sys.exit kill test runs is best for test authors.