在 GoogleTest 中使用 ASSERT 和 EXPECT

发布于 2024-08-27 07:13:06 字数 67 浏览 7 评论 0原文

当 ASSERT_* 宏导致测试用例终止时,EXPECT_* 宏继续其评估。 我想知道决定是否使用其中之一的标准是什么。

While ASSERT_* macros cause termination of test case, EXPECT_* macros continue its evaluation.
I would like to know which is the criteria to decide whether to use one or the other.

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

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

发布评论

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

评论(4

野の 2024-09-03 07:13:06

当条件必须成立时使用ASSERT - 如果不成立,测试将立即停止。当测试的其余部分在不满足此条件的情况下不具有语义意义时,请使用此选项。

当条件应该成立时使用EXPECT,但在不成立的情况下,我们仍然可以通过继续测试获得价值。 (不过,测试最终还是会失败。)

经验法则是:默认使用EXPECT,除非您需要在剩下的时间里保存一些东西。测试,在这种情况下,您应该针对该特定条件使用 ASSERT。


入门中也体现了这一点:

通常首选EXPECT_*,因为它们允许在测试中报告多个失败。但是,如果当相关断言失败时继续没有意义,您应该使用 ASSERT_*

Use ASSERT when the condition must hold - if it doesn't the test stops right there. Use this when the remainder of the test doesn't have semantic meaning without this condition holding.

Use EXPECT when the condition should hold, but in cases where it doesn't we can still get value out of continuing the test. (The test will still ultimately fail at the end, though.)

The rule of thumb is: use EXPECT by default, unless you require something to hold for the remainder of the tests, in which case you should use ASSERT for that particular condition.


This is echoed within the primer:

Usually EXPECT_* are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_* if it doesn't make sense to continue when the assertion in question fails.

在你怀里撒娇 2024-09-03 07:13:06

当您

  • 想要在测试中报告多个失败时,

请使用 EXPECT_

  • 断言失败时继续没有意义时,

请使用 ASSERT_ASSERT_ code> 如果函数失败,则立即中止函数,并跳过可能的清理代码。
首选 EXPECT_ 作为默认值。

Use EXPECT_ when you

  • want to report more than one failure in your test

Use ASSERT_ when

  • it doesn't make sense to continue when the assertion fails

Since ASSERT_ aborts your function immediately if it fails, possible cleanup code is skipped.
Prefer EXPECT_ as your default.

陌伤浅笑 2024-09-03 07:13:06

除了之前的答案之外...

ASSERT_ 不会终止测试用例的执行。它从使用的任何函数中返回。除了测试用例失败之外,它的计算结果为 return;,这意味着它不能在返回 void 以外的函数中使用代码>.除非您对编译器警告感到满意,否则就是这样。

EXPECT_ 测试用例失败,但不return;,因此它可以在任何返回类型的函数内部使用。

In addition to previous answers...

ASSERT_ does not terminate execution of the test case. It returns from whatever function is was used in. Besides failing the test case, it evaluates to return;, and this means that it cannot be used in a function returning something other than void. Unless you're fine with the compiler warning, that is.

EXPECT_ fails the test case but does not return;, so it can be used inside functions of any return type.

荭秂 2024-09-03 07:13:06

ASSERT_*:

  • 当失败时,不会检查其余条件。

EXPECT_*:

  • 失败时,仍检查剩余条件。

ASSERT_*:

  • when it fails, won't check the remaining conditions.

EXPECT_*:

  • when it is failed, it still checks the remaining conditions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文