1

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.

1
  • This looks like it is from a unit-test? Why would you want to catch such an assertion? Also, is this your original code? There was a syntax error in it, and also the indentation is weird. Please show us actual code that reproduces the be behavior. Commented Apr 7, 2011 at 8:09

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

assert is a statement, not a method.
Be careful with the assert statement... if you ever compile your code with the -O (or -OO) option, they all disappear (by design). See the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.