为什么单元测试系统包含无用的断言方法?

发布于 2025-01-06 09:09:22 字数 1432 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病毒体 2025-01-13 09:09:22

这些函数通常会提供更有用的输出。例如,assertEquals 测试可以告诉您预期值和实际值,以及它们不相等。

例如,以下代码:

this->assertEquals(1, 0);

将产生以下输出:

Failed asserting that 0 matches expected 1.

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.
过气美图社 2025-01-13 09:09:22

这也与冗长有关。这:

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

比以下内容简洁得多:

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

或者甚至更好:

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

在其他框架(例如 .NET 的 NUnit)中,它甚至更好:

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

vs

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

阅读最后一个示例几乎就像有人在解释代码你。当您处理旧的/其他人的代码时,这是非常宝贵的。

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.

老街孤人 2025-01-13 09:09:22

原因是能够为失败的测试提供更清晰的消息,例如比较;

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

Test failed: foo should be 2, actual value 5.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文