[英]Using ASSERT and EXPECT in GoogleTest
While ASSERT_* macros cause termination of test case, EXPECT_* macros continue its evaluation.当 ASSERT_* 宏导致测试用例终止时,EXPECT_* 宏继续其评估。 I would like to know which is the criteria to decide whether to use one or the other.我想知道决定是否使用一个或另一个的标准是什么。
Use ASSERT
when the condition must hold - if it doesn't the test stops right there.当条件必须成立时使用ASSERT
- 如果它不成立,测试就在那里停止。 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.当条件应该成立时使用EXPECT
,但在条件不成立的情况下,我们仍然可以从继续测试中获得价值。 (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.经验法则是:默认情况下使用EXPECT
,除非您需要为剩余的测试保留一些东西,在这种情况下,您应该针对该特定条件使用ASSERT
。
This is echoed within the primer :这在引文中得到了回应:
Usually
EXPECT_*
are preferred, as they allow more than one failures to be reported in a test.通常首选EXPECT_*
,因为它们允许在测试中报告多个失败。 However, you should useASSERT_*
if it doesn't make sense to continue when the assertion in question fails.但是,如果在相关断言失败时继续操作没有意义,您应该使用ASSERT_*
。
Use EXPECT_
when you使用EXPECT_
时
Use ASSERT_
when使用ASSERT_
时
Since ASSERT_
aborts your function immediately if it fails, possible cleanup code is skipped.由于ASSERT_
会在失败时立即中止您的函数,因此会跳过可能的清理代码。 Prefer EXPECT_
as your default.首选EXPECT_
作为您的默认值。
In addition to previous answers...除了之前的回答...
ASSERT_
does not terminate execution of the test case. ASSERT_
不会终止测试用例的执行。 It returns from whatever function is was used in. Besides failing the test case, it evaluates to return;
它从使用过的任何函数返回。除了测试用例失败外,它评估return;
, and this means that it cannot be used in a function returning something other than void
. ,这意味着它不能用于返回void
以外的其他内容的函数中。 Unless you're fine with the compiler warning, that is.除非您对编译器警告没有意见,否则。
EXPECT_
fails the test case but does not return;
EXPECT_
测试用例失败但不return;
, so it can be used inside functions of any return type. ,因此它可以在任何返回类型的函数中使用。
While ASSERT_* macros cause termination of test case, EXPECT_* macros continue its evaluation.当ASSERT_ *宏导致测试用例终止时,EXPECT_ *宏继续其评估。 I would like to know which is the criteria to decide whether to use one or the other.我想知道哪个是决定使用一个或另一个的标准。
Google C++
Testing Framework supports two families of assertions with the same interface: Google C++
测试框架支持两个具有相同接口的断言系列:
ASSERT
: Fails fast, aborting the current function.断言:快速失败,中止当前ASSERT
。EXPECT
: Continues after the failure. EXPECT
:失败后继续。 EXPECT
is often more appropriate because it: reveals more failures in a single run, and allows more to be fixed in a single edit/compile/run-tests cycle. EXPECT
通常更合适,因为它: 在单次运行中揭示更多失败,并允许在单次编辑/编译/运行测试循环中修复更多问题。
Example:例子:
TEST(WordStemmerTest, StemsPluralSuffixes)
{
EXPECT_STREQ("jump", stemmer->Stem("jumps"));
EXPECT_STREQ("pixi", stemmer->Stem("pixies"));
EXPECT_STREQ("prioriti", stemmer->Stem("priorities"));
// etc ...
}
Use ASSERT
when it doesn't make sense to continue.当继续下去没有意义时使用ASSERT
。
Example:例子:
TEST(FileGeneratorTest, CreatesFileContainingSequenceOfChars)
{
ASSERT_TRUE(fp = fopen(path, "r")) << "Failed to open " << path;
ASSERT_EQ(10, fread(buffer, 1, 10, fp)) << "File " << path << " is too small"; buffer[10] = '\0';
EXPECT_STREQ("123456789", buffer) << "File " << path << " is corrupted";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.