Why do Unit Test systems include useless assertive methods? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-05 13:15:41

These functions usually give more useful output. For example, an assertEquals test could tell you the expected and actual values, and that they were not equal.

For example, the following code:

this->assertEquals(1, 0);

Will produce this output:

Failed asserting that 0 matches expected 1.

It's also about verbosity. This:

this->assert(foo == array_pop(stack));

Is much less verbose than:

this->assertEqual(foo, array_pop(stack));

Or even better:

$popped_value = array_pop(stack);
this->assertEqual($popped_value, foo);

In other frameworks (such as .NET's NUnit), it gets even better:

Assert.AreEqual(stack.Pop(), foo);

vs

var poppedValue = stack.Pop();
Assert.That(poppedValue, Is.EqualTo(foo));

Reading the last example is almost as if somebody was explaining the code to you. That's invaluable when you deal with old/somebody's else code.

The reason is afaik to be able to present cleaner messages for failing tests, compare for example;

Test failed: 'foo' == array_pop(stack) assertion failed

to

Test failed: foo should be 2, actual value 5.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!