如何更改 Python AssertionError 中的消息?

发布于 2024-09-25 00:14:03 字数 689 浏览 7 评论 0原文

我正在按照以下内容编写,其中我尝试在比较两个多行 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' != 'something else',它只显示输出的第一行。

如何更改断言消息以打印出我想要的任何内容?

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

I'm writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the comparison raises an assertion, but the default explanation is useless to me

I need to add something to code such as this below:

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

I cannot figure out how to change the printed error message in the assertionerror I catch. I always get AssertionError: u'something' != 'something else', which only shows the first line of the output.

How can I change the assertion message to print out whatever I want?

If it's relevant, I am using nose to run the test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忱杏 2024-10-02 00:14:03
assert expression, info

例如,

>>> assert False, "Oopsie"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Oopsie

来自 文档

断言语句是一种便捷的方式
将调试断言插入到
程序:

assert_stmt ::= "断言" 表达式
[“,“ 表达] 

简单的形式,
断言表达式,相当于

如果 __debug__:
    如果不是表达式:
        引发断言错误 

扩展形式

断言表达式1、表达式2

相当于

如果 __debug__:
    如果不是表达式1:
        引发断言错误(表达式2)

这些等价假设
__debug__AssertionError 使用这些引用内置变量
名称。在当前的实现中,
内置变量 __debug__ 是
一般情况下为真,为假
当需要优化时
(命令行选项 -O)。目前的
代码生成器不发出任何代码
优化时断言语句
在编译时请求。注意
没有必要包括
表达式的源代码
错误消息中失败;它将
显示为堆栈的一部分
跟踪。

assert expression, info

For instance,

>>> assert False, "Oopsie"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Oopsie

From the docs:

Assert statements are a convenient way
to insert debugging assertions into a
program:

assert_stmt ::=  "assert" expression
["," expression] 

The simple form,
assert expression, is equivalent to

if __debug__:
    if not expression:
        raise AssertionError 

The extended form

assert expression1, expression2

is equivalent to

if __debug__:
    if not expression1:
        raise AssertionError(expression2)

These equivalences assume that
__debug__ and AssertionError refer to the built-in variables with those
names. In the current implementation,
the built-in variable __debug__ is
True under normal circumstances, False
when optimization is requested
(command line option -O). The current
code generator emits no code for an
assert statement when optimization is
requested at compile time. Note that
it is unnecessary to include the
source code for the expression that
failed in the error message; it will
be displayed as part of the stack
trace.

还给你自由 2024-10-02 00:14:03

使用e.argse.message已弃用。

try:
    assert False, "Hello!"
except AssertionError as e:
    e.args += ('some other', 'important', 'information', 42)
    raise

这保留了原始的回溯。它的最后一部分如下所示:

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

Works in those Python 2.7 and Python 3.

Use e.args, e.message is deprecated.

try:
    assert False, "Hello!"
except AssertionError as e:
    e.args += ('some other', 'important', 'information', 42)
    raise

This preserves the original traceback. Its last part then looks like this:

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

Works in both Python 2.7 and Python 3.

请爱~陌生人 2024-10-02 00:14:03

您想要获取捕获的异常,将其转换为字符串,将其与一些附加字符串信息组合,然后引发新的异常。

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

You want to take the caught exception, convert it to a string, combine it with some additional string info, and raise a new exception.

x = 3
y = 5
try:
    assert( x == y )
except AssertionError, e:
    raise( AssertionError( "Additional info. %s"%e ) )
り繁华旳梦境 2024-10-02 00:14:03

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

raise AssertionError(line1 + ' != ' + line2)

希望这有帮助。

You can pass the desired message when creating the exception.

raise AssertionError(line1 + ' != ' + line2)

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文