开发者

Handling assertion in python

I can't understand why this code:

x='aaaa'
try: 
    self.assertTrue(x==y)
except: 
    print (x)

generates me this error

AssertionError: False is not True

It should be handle it by

print(x)

EDIT

original code is:

try:
    self.assertTrue('aaaa'==self.compare_object_name[1])
except:
    print ('aaa')

@Space_C0wb0y I can't give you full code because it 开发者_如何学Cis not my code, and I don't have a permission.


You should include the code that defines the assertTrue method. From the output you get, I'd say that it actually does not throw an exception, but deals with it internally (thus the error message being printed, and not your value).

You can use the built-in assert statement of Python, which works as expected:

x = 'aaaa'
y = 'bbb'

try:
    assert(x == y)
except:
    print (x)

Output:

>>> 
aaaa
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜