I have a simple python function.
def test():
print "test"
sys.exit(1)
I am using python 2.6. How can I create unittest for this function? This is because sys.exit can be handled in unit test cases after python 2.7.
can anyone let me know how to create unittest for this simple code?
According to what I can see there's been little change in sys.exit during the 2.x. The only difference I can see is that earlier the sys.exit didn't check it's argument, but rather raised SystemExit exception anyway (now it only raises it if you supply a proper argument).
So for example I tried the following using python 2.6 (also known as 2.6.0):
import sys
try:
sys.exit(1)
except SystemExit(1) as e:
print repr(e)
and it responded with SystemExit(1,)
So your test should be something like:
def test():
passed = False
try:
sys.exit(1)
except SystemExit as e:
if e.code == 1:
passed = True
assert passed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With