如何在Python断言错误中更改消息?

如何在Python断言错误中更改消息?,python,exception,assertions,nose,Python,Exception,Assertions,Nose,我是按照以下内容编写的,在其中,当比较两个Unicode文本的多行块时,我尝试生成一条适当的错误消息。进行比较的内部方法提出了一个断言,但是默认的解释对我来说是无用的 我需要在代码中添加如下内容: def assert_long_strings_equal(one, other): lines_one = one.splitlines() lines_other = other.splitlines() for line1, line2 in zip(lines_one,

我是按照以下内容编写的,在其中,当比较两个Unicode文本的多行块时,我尝试生成一条适当的错误消息。进行比较的内部方法提出了一个断言,但是默认的解释对我来说是无用的

我需要在代码中添加如下内容:

def assert_long_strings_equal(one, other):
    lines_one = one.splitlines()
    lines_other = other.splitlines()
    for line1, line2 in zip(lines_one, lines_other):
        try:
            my_assert_equal(line1, line2)
        except AssertionError, error:
            # Add some information to the printed result of error??!
            raise
我不知道如何更改我捕获的assertionerror中打印的错误消息。我总是得到
断言错误:u'something'!='另一个“
,它只显示输出的第一行

如何更改断言消息以打印出所需内容?


如果相关,我将使用
nose
运行测试。

您可以在创建异常时传递所需的消息

raise AssertionError(line1 + ' != ' + line2)
x = 3
y = 5
try:
    assert( x == y )
except AssertionError, e:
    raise( AssertionError( "Additional info. %s"%e ) )
希望这有帮助

assert expression, info
比如说,

>>> assert False, "Oopsie"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Oopsie
简单的形式,
断言表达式
,相当于

if __debug__:
    if not expression:
        raise AssertionError 
if __debug__:
    if not expression1:
        raise AssertionError(expression2)
扩展形式

assert expression1, expression2
相当于

if __debug__:
    if not expression:
        raise AssertionError 
if __debug__:
    if not expression1:
        raise AssertionError(expression2)
这些等价性假设
\uuuu debug\uuu
AssertionError
指的是具有这些属性的内置变量 名字。在目前的实施中,, 内置变量
\uuuu debug\uuuu
为 正常情况下正确,错误 当需要优化时 (命令行选项-O)。电流 代码生成器不会为一个 执行优化时的assert语句 在编译时请求。注意 没有必要包括以下内容: 表达式的源代码 在错误消息中失败;会的 将显示为堆栈的一部分 跟踪


您希望获取捕获的异常,将其转换为字符串,将其与其他字符串信息组合,并引发新异常

raise AssertionError(line1 + ' != ' + line2)
x = 3
y = 5
try:
    assert( x == y )
except AssertionError, e:
    raise( AssertionError( "Additional info. %s"%e ) )

使用
e.args
e.message
不推荐使用

try:
    assert False, "Hello!"
except AssertionError as e:
    e.args += ('some other', 'important', 'information', 42)
    raise
这将保留原始回溯。其最后一部分如下所示:

AssertionError: ('Hello!', 'some other', 'important', 'information', 42)

Python2.7和Python3都可以使用。

使用此方法,我可以编辑消息,并且仍然可以看到堆栈跟踪(+任何其他信息)。此外,换行符也显示在右侧

try:
   my_assert_equal(line1, line2)
except AssertionError as e:
   message = e.args[0]
   message += "\nThis appends the default message and can have newlines"
   e.args = (message,) #wrap it up in new tuple
   raise

我只是想澄清一下,我意识到捕捉断言错误是很奇怪的。恰巧
my_assert_equal
有点深奥,我不想弄乱它。我要指出的是,你应该有
,除了
而不是
catch
。虽然我确信这只是一个输入错误:p这不是OP的意思
AssertionError
通常由
assert
语句引发。我不确定。。。但是,如果用
if
语句代替
assert
进行包装,则可能是可以接受的。通过这种方式,您可以使用
if else
块,并使用自定义消息回退到断言错误上。请注意,您还可以在
info
字符串表达式中嵌入换行符,以使其在显示时看起来更美观。还请注意,您可以通过使用Python的字符串插值和更新的字符串格式化操作。通常引发异常不是问题。我问的是捕捉、修改并重新引发一个表达式。@Andres--你应该编辑这个问题,明确地说。。。我不认为这个问题是这么说的。拍额头。现在很清楚为什么
assert(condition,message)
会出现一个始终为真的警告:assert是一个运算符而不是一个函数,所以该语句测试一个元组。相反,
assert condition message
我没有看到任何发布的答案提供了如何向异常添加信息并使用原始信息和附加信息重新提出的一般解决方案,我认为这是OP所要求的。Russel是对的,这就是我一直在寻找的东西。当我在2.7上尝试它时,这只会引发一个类型错误。哪种类型的错误?我在写答案时使用的是2.7。@philologon我刚刚在Python2.7.10和Python3.5.1中尝试了这个解决方案(完全复制粘贴我的答案),效果如预期。我的答案被否决了,但我看不出有什么原因。@Honza Javorek这是一个不错的解决方案,但可能有一个与某个库相关的行吗?@matanster与print不同,assert在Python 3中仍然是一个语句,它包括换行符,不必在
+=
上复制:
e.args=(u”%s\n%s%%(e.args[0],addl\u message),