【发布时间】:2011-04-17 23:40:47
【问题描述】:
我正在编写以下内容,在比较两个多行 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 中打印的错误消息。我总是得到AssertionError: u'something' != 'something else',它只显示输出的第一行。
如何更改断言消息以打印出我想要的任何内容?
如果相关,我将使用nose 运行测试。
【问题讨论】:
-
澄清一下,我意识到捕获断言错误很奇怪。碰巧
my_assert_equal有点深,我不想惹它。 -
只是指出,你应该有
except而不是catch。虽然我确定这只是一个错字:p
标签: python exception assertions nose