expect_fatal_failure 与普通断言之间的区别
Difference between expect_fatal_failure & plain assertion
首先,我很抱歉我的英语不好。
在 Google 测试的 github 中,它以这种方式解释 expect_fatal_failure 断言:
EXPECT_FATAL_FAILURE(statement, substring); to assert that statement
generates a fatal (e.g. ASSERT_*) failure whose message contains the
given substring
但是当我 运行 我的 expect_fatal_failure 项目时,我找不到使用 EXPECT_NONFATAL_FAILURE(statement, substring) 执行语句和只执行语句
之间的区别]
这是我的代码,
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
void failTwice()
{
EXPECT_TRUE(false) << "fail first time";
ASSERT_TRUE(false) << "fail second time";
}
TEST(FailureTest, FirstTest)
{
EXPECT_NONFATAL_FAILURE(failTwice(), "time");
failTwice();
}
TEST(FailureTest, SecondTest)
{
EXPECT_NONFATAL_FAILURE(failTwice(), "second");
failTwice();
}
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
结果在图片中。
我这样做有什么问题吗?
或者它们之间没有任何区别?
输出基本上是在告诉您发生了什么:EXPECT_NONFATAL_FAILURE
语句告诉 Google 测试预计在调用 FailTwice
时会发生一次失败,但它会产生两次。注释该函数的第二行并删除对 FailTwice
的额外调用,两个测试都会通过,例如
void failNonFatallyOnFalse(bool param)
{
EXPECT_TRUE(param) << "fail non-fatally";
}
void failFatallyOnFalse(bool param)
{
ASSERT_TRUE(param) << "fail fatally";
}
TEST(FailureTest, TestFailNonFatally)
{
EXPECT_NONFATAL_FAILURE(failNonFatallyOnFalse(false), "fail non-fatally");
}
TEST(FailureTest, TestFailFatally)
{
EXPECT_FATAL_FAILURE(failFatallyOnFalse(false), "fail fatally");
}
你可以把它们想象成转败为成功,转成功为失败。
乍一看这似乎不是很有用,但是文档explains what these macros are for: they are useful if you are building your own assertions, for example using predicate formatters。您使用不同的参数在 EXPECT_NONFATAL_FAILURE
或 EXPECT_FATAL_FAILURE
下编写您的断言和 运行 并验证它是否产生预期输出的失败。如果你只是使用 Google Test 编写常规测试,则不需要这两个断言。
这是我们想出的解决方案。 I would mark as duplicate to point to the other question,但我缺乏声望。:
//adapted from EXPECT_FATAL_FAILURE
do {
//capture all expect failures in test result array
::testing::TestPartResultArray gtest_failures;
//run method in its own scope
::testing::ScopedFakeTestPartResultReporter gtest_reporter(
::testing::ScopedFakeTestPartResultReporter::
INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);
//run your method
failTwice();
//only check on number, this will include fatal and nonfatal, but if you just care about number then this is sufficient
ASSERT_EQ(gtest_failures.size(), 2) << "Comparison did not fail FATAL/NONFATAL twice";
} while (::testing::internal::AlwaysFalse());
首先,我很抱歉我的英语不好。
在 Google 测试的 github 中,它以这种方式解释 expect_fatal_failure 断言:
EXPECT_FATAL_FAILURE(statement, substring); to assert that statement generates a fatal (e.g. ASSERT_*) failure whose message contains the given substring
但是当我 运行 我的 expect_fatal_failure 项目时,我找不到使用 EXPECT_NONFATAL_FAILURE(statement, substring) 执行语句和只执行语句
之间的区别]这是我的代码,
#include "gtest/gtest.h"
#include "gtest/gtest-spi.h"
void failTwice()
{
EXPECT_TRUE(false) << "fail first time";
ASSERT_TRUE(false) << "fail second time";
}
TEST(FailureTest, FirstTest)
{
EXPECT_NONFATAL_FAILURE(failTwice(), "time");
failTwice();
}
TEST(FailureTest, SecondTest)
{
EXPECT_NONFATAL_FAILURE(failTwice(), "second");
failTwice();
}
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
结果在图片中。
我这样做有什么问题吗?
或者它们之间没有任何区别?
输出基本上是在告诉您发生了什么:EXPECT_NONFATAL_FAILURE
语句告诉 Google 测试预计在调用 FailTwice
时会发生一次失败,但它会产生两次。注释该函数的第二行并删除对 FailTwice
的额外调用,两个测试都会通过,例如
void failNonFatallyOnFalse(bool param)
{
EXPECT_TRUE(param) << "fail non-fatally";
}
void failFatallyOnFalse(bool param)
{
ASSERT_TRUE(param) << "fail fatally";
}
TEST(FailureTest, TestFailNonFatally)
{
EXPECT_NONFATAL_FAILURE(failNonFatallyOnFalse(false), "fail non-fatally");
}
TEST(FailureTest, TestFailFatally)
{
EXPECT_FATAL_FAILURE(failFatallyOnFalse(false), "fail fatally");
}
你可以把它们想象成转败为成功,转成功为失败。
乍一看这似乎不是很有用,但是文档explains what these macros are for: they are useful if you are building your own assertions, for example using predicate formatters。您使用不同的参数在 EXPECT_NONFATAL_FAILURE
或 EXPECT_FATAL_FAILURE
下编写您的断言和 运行 并验证它是否产生预期输出的失败。如果你只是使用 Google Test 编写常规测试,则不需要这两个断言。
这是我们想出的解决方案。 I would mark as duplicate to point to the other question,但我缺乏声望。:
//adapted from EXPECT_FATAL_FAILURE
do {
//capture all expect failures in test result array
::testing::TestPartResultArray gtest_failures;
//run method in its own scope
::testing::ScopedFakeTestPartResultReporter gtest_reporter(
::testing::ScopedFakeTestPartResultReporter::
INTERCEPT_ONLY_CURRENT_THREAD, >est_failures);
//run your method
failTwice();
//only check on number, this will include fatal and nonfatal, but if you just care about number then this is sufficient
ASSERT_EQ(gtest_failures.size(), 2) << "Comparison did not fail FATAL/NONFATAL twice";
} while (::testing::internal::AlwaysFalse());