Hello,
I am runninig test's with Python Unittest. I am running tests but I want to do negative testing and I would like to test if a function throw's an exception, it passes but if no exception is thrown the test fail's. The script I have is:
try:
result = self.client.service.GetStreamUri(self.stream, self.token)
self.assertFalse
except suds.WebFault, e:
self.assertTrue
else:
self.assertTrue
This alway's passes as True even when the function work's perfectly. I have also tried various other way's including:
try:
result = self.client.service.GetStreamUri(self.stream, self.token)
self.assertFalse
except suds.WebFault, e:
self.assertTrue
except Exception, e:
self.assertTrue
Does anyone have any suggestions?
Thanks
I have tried assertRaises with no luck.
try:
result = self.client.service.GetStreamUri(self.stream, self.token)
self.assertRaises(WebFault)
except suds.WebFault, e:
self.assertFalse
except Exception, e:
self.assertTrue
It still passes. For some reason it does not try and do the assertRaises
statement. I have also tried: (The function should fail but the test should pass)
try:
result = self.client.service.GetStreamUri(self.stream, self.token)
except suds.WebFault, e:
self.assertFalse
except Exception, e:
self.assertTrue
else:
self.assertFalse
For some reason even when the function passes it will not raise an error. It always no matter what goes to Exception. Unless there is an else statement then it goes to that.
Found a way that work's but it seem's a very poor way of doing things:
Can anyone suggest a cleaner way of doing this?
try:
result = self.client.service.GetStreamUri(self.stream, self.token)
except suds.WebFault, e:
self.assertFalse
except Exception, e:
pass
try:
result==result
except:
result=None
if result==None:
assert True
else:
assert False