简体   繁体   English

assertionerror在python中返回空字符串

[英]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. 它除了xy外什么都不打印。 no class name or anything else. 没有班级名称或其他任何东西。 what does the error in AssertionError normally contain? AssertionError中的错误通常包含什么?

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. 根本没有发送消息,因此它将一堆空字符串附加到verificationErrors。

Standard assert statement doesn't put anything into the AssertionError , it's the traceback that matters. 标准的assert语句没有将任何内容放入AssertionError ,重要的是回溯。 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. 有一个assert expr, msg的变种,设置错误消息,如果你使用单元测试,那么第二个参数assertTruefailUnless已过时)将做到这一点。

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. unittest模块中的所有断言都采用最终参数msg ,如果断言失败,则该参数是要引发的消息。 Put your debugging there if necessary. 如有必要,请将调试放在那里。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM