user278618
user278618

Reputation: 20262

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 is not my code, and I don't have a permission.

Upvotes: 1

Views: 5160

Answers (1)

Wookai
Wookai

Reputation: 21783

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

Upvotes: 4

Related Questions