TestNG 中 assertEquals() 与 assertTrue() 之间的实际区别是什么? [英] What is the actual difference between assertEquals() vs assertTrue() in TestNG?
问题描述
我对这两种方法感到困惑,因为两者都可以做同样的事情,就像下面我的代码片段一样.
I'm confusing about both these methods, because both can do the same thing, like below snippet of my code.
使用assertEquals()
String a = "Hello";
String b = "Hello";
assertEquals(a, b);
使用assertTrue()
assertTrue(a.equals(b));
谁能告诉我这两种方法之间的实际区别?
Can anyone tell me the actual difference between both these two methods?
推荐答案
assertEquals
更好,因为它为单元测试框架提供了更多关于你真正感兴趣的信息.这使它能够提供测试失败时更好的错误信息.
assertEquals
is better because it gives the unit test framework more information about what you're actually interested in. That allows it to provide better error information when the test fails.
假设你有
String a = "Hello";
String b = "Hi";
然后测试失败可能看起来像:
Then the test failures might look something like:
// From assertEquals(a, b)
Error: Expected "Hi"; was "Hello"
// From assertTrue:
Error: Expected true; was false
考虑到这些值可能是相当复杂的计算的结果,您认为哪些可以为您提供更多信息?
Which of those do you think gives you more information, bearing in mind that the values would probably be the result of reasonably complex computations?
(这些是错误消息,因为我没有安装 testng,但它们是单元测试框架提供的种类.)
(These are made up error messages as I don't have testng installed, but they're the kind of thing unit test frameworks give.)
这篇关于TestNG 中 assertEquals() 与 assertTrue() 之间的实际区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!