测试参数化 JUnit 4+ 中的可选异常测试
我正在尝试为一个方法编写一个单元测试,该方法将字符串作为参数 如果格式错误则抛出异常(如果没问题则抛出异常)。 我想编写一个参数化测试,它输入几个字符串和 预期的异常(包括如果输入没有抛出异常的情况) 字符串格式正确!)。如果尝试使用 @Test(expect=SomeException.class) 注解中,我遇到了两个问题:
expect=null 是不允许的。 那么我如何测试没有抛出异常的预期结果 (对于格式正确的输入字符串)?
期望=不可能吗? 我还没有尝试过,但我强烈怀疑之后的情况就是这样 阅读本文(请说明这是否属实?): http://tech.groups.yahoo.com/group/junit/message/ 19383 这似乎是我发现的最好的解决方案。你觉得怎么样 它,特别是与以下相比: 如何在参数化测试中测试异常?
预先感谢您的任何帮助,我期待讨论:)
I am trying to write a unit test for a method which takes a string as a para-
meter and throws an exception if it is malformed (AND NONE if it is okay).
I want to write a parameterized test which feeds in several strings and the
expected exception (INCLUDING the case that none is thrown if the input
string is well-formed!). If trying to use the @Test(expect=SomeException.class)
annotation, I encountered two problems:
expect=null is not allowed.
So how could I test for the expected outcome of NO exception to be thrown
(for well-formed input strings)?expect= not possible?
I not yet tried it, but I strongly suspect that this is the case after
reading this (could you please state whether this is true?):
http://tech.groups.yahoo.com/group/junit/message/19383
This then seems to be the best solution I found yet. What do you think about
it, especially compared to that:
How do I test exceptions in a parameterized test?
Thank you in advance for any help, I look forward the discussion :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建两个测试用例类:
显然第一个测试所有类型的有效输入(不抛出异常),而第二个总是期望异常。
请记住:测试的可读性比生产代码的可读性更重要。不要在 JUnit 测试用例中使用古怪的标志、条件和逻辑。简单就是王道。
另请参阅我的回答此处 获取如何干净地测试异常的提示。
Create two test case classes:
Obviously the first one tests all sorts of valid inputs (not throwing an exception), whilst the second one always expects the exception.
Remember: readability of your tests is even more important than readability of production code. Don't use wacky flags, conditions and logic inside JUnit test cases. Simplicity is the king.
Also see my answer here for a hint how to test for exceptions cleanly.
有两种不同的测试 - 一种用于有效输入,另一种用于无效输入。我没有使用过 JUnit 4,所以我无法评论确切的注释格式 - 但基本上你会有一个带有各种不同无效输入的参数化测试,这表明它确实期望出现异常,以及使用各种不同的有效输入进行的单独测试,其中没有说明任何有关异常的信息。如果当您的测试没有说明应该抛出异常时抛出异常,则测试将失败。
Have two different tests - one for valid inputs and one for invalid ones. I haven't used JUnit 4 so I can't comment on the exact annotation format - but basically you'd have one parameterized test with various different invalid inputs, which says that it does expect an exception, and a separate test with various different valid inputs which doesn't say anything about exceptions. If an exception is thrown when your test doesn't say that it should be, the test will fail.
在许多情况下,将测试用例分为两个测试类是适当的方法 - 正如 Tomasz 和 Jon 已经概述的那样。
但在其他情况下,仅就可读性而言,这种拆分并不是一个好的选择。我们假设测试数据集中的行具有自然顺序,如果行按此自然顺序排序,则可以很容易地看出测试数据是否涵盖所有相关用例。如果将测试用例分为两个测试类,则不再有一种简单的方法来查看是否覆盖了所有相关的测试用例。对于这些情况
如何在参数化测试中测试异常?
似乎确实提供了最好的解决方案。
Splitting the test cases into two test classes is the appropriate approach in many cases - as both Tomasz and Jon already outlined.
But there are other cases where this split is not a good choice just in terms of readability. Let's assume the rows in the tested data set have a natural order and if the rows are sorted by this natural order it may be easy to see whether or not the test data covers all relevant use cases. If one splits the test cases into two test classes, there is no longer an easy way to see whether all relevant test cases are covered. For these cases
How do I test exceptions in a parameterized test?
seeems to provide the best solution indeed.