5

I am doing this:

try: self.failUnless(sel.is_text_present("F!")) #sel.is_text_present("F!") is false
except AssertionError, e: 
    print("x"+e+"y")
    sys.exit()

it is printing nothing except xy. no class name or anything else. what does the error in AssertionError normally contain?

edit: apparently the user provides its own message. selenium generated many of these:

except AssertionError, e: self.verificationErrors.append(str(e))

without sending in a message at all, so it appends a bunch of empty strings to verificationErrors.

1
  • What's worse, any exception without a message will have empty representation, e.g. raise ValueError Commented Aug 4, 2020 at 16:48

3 Answers 3

4

Don't catch the errors from assertions. All the assertions in the unittest module take a final parameter, msg, which is the message to be raised if the assertion fails. Put your debugging there if necessary.

Sign up to request clarification or add additional context in comments.

1 Comment

nooooooooooo. i have 30 files with lots of asserts generated from selenium. all of them are like this. whenever an assert fails for a particular test, i'm left dumbfounded as to the reason the assert failed.
4

Standard assert statement doesn't put anything into the AssertionError, it's the traceback that matters. There is a assert expr, msg variant that sets the error message, if you're using unittest, then the second argument of assertTrue (failUnless is deprecated) will do it.

Comments

2

Sounds like you want to use your assertions as debug statements in the event of a failure. This should help...

import traceback

try:
  assert 1 == 2
except AssertionError: 
  traceback.print_exc()

This prints:

Traceback (most recent call last):
  File "./foo.py", line 4, in <module>
    assert 1 == 2

Comments

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.