JUnit - 使用错误的断言
我引用 exubero 条目中的这篇文章。我认为这篇文章将使每个进行单元测试的人受益:
Junit 的 Assert 类中定义了许多以断言开头的不同方法。这些方法中的每一种对于它们所断言的内容都有稍微不同的参数和语义。
下面展示了assertTrue的一些不规则使用:
assertTrue("Objects must be the same", expected == actual);
assertTrue("Objects must be equal", expected.equals(actual));
assertTrue("Object must be null", actual == null);
assertTrue("Object must not be null", actual != null);
一些单元测试专家指出,上面的代码可以更好地写成:
assertSame("Objects must be the same", expected, actual);
assertEquals("Objects must be equal", expected, actual);
assertNull("Object must be null", actual);
assertNotNull("Object must not be null", actual);
使用适当的'assertXXX()'的好处之一是增加单元测试的可读性。谁能指出使用适当的 'assertXXX()' 还有什么其他好处?
I quote this post from exubero's entry. I think this entry will benefit everyone who is doing a unit test:
There are a large number of different methods beginning with assert defined in Junit's Assert class. Each of these methods has slightly different arguments and semantics about what they are asserting.
The following shows some irregular uses of assertTrue:
assertTrue("Objects must be the same", expected == actual);
assertTrue("Objects must be equal", expected.equals(actual));
assertTrue("Object must be null", actual == null);
assertTrue("Object must not be null", actual != null);
Some unit testing experts pointed out that the above code could be better written as:
assertSame("Objects must be the same", expected, actual);
assertEquals("Objects must be equal", expected, actual);
assertNull("Object must be null", actual);
assertNotNull("Object must not be null", actual);
One of the advantage of using the appropriate 'assertXXX()' will increase the readability of the unit test. Can anyone point out what other benefit of using the appropriate 'assertXXX()' ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 Java 开发人员,我不知道断言失败时 JUnit 会输出什么。
我一直在使用的许多单元测试框架在使用像assertEquals这样的东西时会输出更好的错误信息。
让我向您展示我正在谈论的一个示例:
在第一种情况下,您可能会得到如下错误输出:
错误:预期为真,实际为假。
第二种情况的输出:
错误:执行的“一”实际为“二”。
正如您所看到的,第二种情况提供了更好、更有意义的信息。
I'm not a Java developer and I don't know what JUnit outputs when an assertion fails.
Many unit testing frameworks that I've been using output better error information when use something like assertEquals.
Let me show you an example what I'm talking about:
In first case you can have an error output like this:
Error: Expected true actual was false.
Output for the second case:
Error: Exected "One" actual was "Two".
As you can see the second case gives better more meaningful information.
除了上面@Vadim 所说的之外,使用正确的断言可以防止由于测试的剪切复制粘贴而产生的错误。
作为一个例子
,然后复制并修改为
当代码更改并且此测试失败并且注释欺骗下一个开发人员以引入新错误的方式“修复”代码时。
如果剪切-复制-粘贴代码是这样的:
注释、断言和测试用例的不一致可能会更明显。
是的,我已经看到这种情况发生了。
In addition to what @Vadim daid above, using the proper assert may guard against bugs created by cut-copy-paste of tests.
As an example
Is then copied and modified to
When the code changes and this test fails and the comment fools the next developer to "fix" the code in a way that introduces a new bug.
If the cut-copy-paste-code was something like this:
The dis-congruity of the comment, the assertion and the test case may be more noticeable.
And yes, I've seen this happen.