我们应该如何用鼻子测试异常?
我正在用鼻子测试异常。这是一个例子:
def testDeleteUserUserNotFound(self):
"Test exception is raised when trying to delete non-existent users"
try:
self.client.deleteUser('10000001-0000-0000-1000-100000000000')
# make nose fail here
except UserNotFoundException:
assert True
如果引发异常,则执行断言,但如果没有引发异常,则不会执行。
我可以在上面的注释行添加任何内容,这样如果没有引发异常,鼻子就会报告失败吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
nose 提供了测试异常的工具(就像unittest一样)。
中的其他工具
尝试这个例子(并阅读鼻子测试工具 会认为这是您正在寻找的正确方法,因为它可以让您具体了解您期望或想要的异常,因此您实际上会引发错误以查看它是否引发了正确的异常。然后你让nose 评估结果(在单元测试中加入尽可能少的逻辑!)
nose provides tools for testing exceptions (like unittest does).
Try this example (and read about the other tools at Nose Testing Tools
I would think that this is the proper way that you were looking for because it lets you be specific about the exceptions that you expect or want. So you actually provoke the error to see that it raises the right exception. And then you let nose evaluate the result. (Put as little logic into the unit tests as possible!)
我强烈建议使用
nose.tools
中的assert_raises
和assert_raises_regexp
,它们复制了assertRaises
和assertRaisesRegexp
来自unittest .TestCase
。这些允许在实际不使用unittest.TestCase
类的测试套件中使用与unittest.TestCase
提供的相同功能。我发现
@raises
是一种过于生硬的工具。下面是说明问题的代码:test3
通过,但不是因为foo
引发了我们期望的异常,而是因为设置要由使用的数据的代码>foo
因相同的异常而失败。test4
展示了如何使用assert_raises
编写测试来实际测试我们要测试的内容。第一行的问题将导致 Nose 报告错误,然后我们可以重写测试,以便该行最终可以测试我们想要测试的内容。@raises
不允许测试与异常关联的消息。当我提出ValueError
时,仅举一个例子,我通常希望通过一条信息性消息来提出它。下面是一个示例:使用
@raises
的test5
将通过,但它会因错误的原因而通过。test6
执行了更精细的测试,结果表明引发的ValueError
不是我们想要的。I strongly recommend using
assert_raises
andassert_raises_regexp
fromnose.tools
, which duplicate the behavior ofassertRaises
andassertRaisesRegexp
fromunittest.TestCase
. These allow using the same functionality as provided byunittest.TestCase
in test suites that do not actually use theunittest.TestCase
class.I find that
@raises
is much too blunt an instrument. Here is code illustrating the problem:test3
passes, but not becausefoo
has raised the exception we were expecting but because the code that sets up the data to be used byfoo
fails with the same exception.test4
shows how the test can be written usingassert_raises
to actually test what we mean to be testing. The problem on the first line will cause Nose to report an error and then we can rewrite the test so that that line so that we can finally test what we did mean to test.@raises
does not allow testing the message associated with the exception. When I raiseValueError
, just to take one example, I usually want to raise it with an informative message. Here's an example:test5
which uses@raises
will pass, but it will pass for the wrong reason.test6
performs a finer test which reveals that theValueError
raised was not the one we wanted.try
/except
的语义意味着执行流程在发生异常时离开try
块,因此assert False
如果引发异常,则不会运行。此外,在except
块运行完成后,执行不会再次重新进入try
块,因此您不会遇到麻烦。The semantics of
try
/except
imply that the flow of execution leaves thetry
block on an exception, soassert False
will not run if an exception is raised. Also, execution will not re-enter thetry
block again after theexcept
block is done running, so you shouldn't run into trouble.我不知道为什么它还不在这里,但以另一种方式存在:
I do not know why it is not here yet but exist one more way:
使用assert_raises:
在测试中使用try和catch似乎是不好的做法(大多数情况)。
鼻子中没有具体的文档,因为它基本上只是 unittest 的包装器.TestCase.assertRaises(参考。如何使用nose的assert_raises?)
Use
assert_raises
:Using try and catch in your tests seems like bad practice (most cases).
There's no specific documentation in nose because it's basically just a wrapper around unittest.TestCase.assertRaises (ref. How to use nose's assert_raises?)
我不知道鼻子是什么,但是你尝试过在 except 子句后面使用“else”吗? IE
I don't know what nose is, but have you tried using 'else' after the except clause. I.e.