开发者

assertionerror returning empty string in python

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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜