assertionerror在python中返回空字符串

时间:2011-02-15 20:39:04

标签: python assert

我这样做:

try: self.failUnless(sel.is_text_present("F!")) #sel.is_text_present("F!") is false
except AssertionError, e: 
    print("x"+e+"y")
    sys.exit()
除了xy之外什么也没打印。没有班级名称或其他任何东西。 AssertionError中的错误通常包含什么?

编辑:显然用户提供了自己的消息。硒产生了许多这些:

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

根本没有发送消息,因此它将一堆空字符串附加到verificationErrors。

3 个答案:

答案 0 :(得分:4)

标准assert语句没有将任何内容放入AssertionError,这是重要的追溯。有一个assert expr, msg变体设置错误消息,如果你使用unittest,那么assertTrue的第二个参数(failUnless已被弃用)将会这样做。

答案 1 :(得分:4)

不要从断言中捕获错误。 unittest模块中的所有断言都采用最后一个参数msg,这是断言失败时要引发的消息。如有必要,请将调试放在那里。

答案 2 :(得分:1)

听起来您希望在发生故障时将断言用作调试语句。这应该有帮助...

import traceback

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

打印:

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