Python 单元测试:让鼻子显示失败的断言值
是否可以显示失败的断言值?它显示了回溯以及抛出了哪种异常,但知道哪些值失败会更实用。
例子:
assert result.file == file
AssertionError
is it possible to show the assertion values that failed? It shows the traceback and what kind of exception was throw but it would more practical to know which values failed.
Example:
assert result.file == file
AssertionError
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该运行nosetests -d,这将显示断言中比较失败的对象的值。
You should run nosetests -d this will display the values of the objects that fail the compare in assert.
assert result.file == file, "%s != %s" % (result.file, file,)
这就是为什么丑陋的
self.assert
方法是在unittest.TestCase
中引入,而不是漂亮而简短的断言:self.assert
方法知道如何显示失败消息。顺便说一句,我认为鼻子做了一些黑魔法,所以在简单的情况下
assert a == b
应该显示有意义的错误消息。
assert result.file == file, "%s != %s" % (result.file, file,)
That's why ugly
self.assert<Foo>
methods were introduced inunittest.TestCase
instead of nice and short asserts:self.assert<Foo>
methods know how to display failure messages.By the way, I thought that nose do some black magic so in simple cases
assert a == b
should show meaningful error message.
另一种可能性:定义自己的函数来完成这个技巧:
您可以调用它而不是
assert
:这会返回这个不错的错误:
Another possibility: define your own function that does the trick:
You can call it instead of
assert
:And this returns this nice error: