GoogleTest初探(1)
此篇主要了解一下GoogleTest中的断言。
总的来说,GoogleTest中的断言分为两大类:EXPECT_*和ASSERT_*,这两者在测试成功或失败后均会给出测试报告,区别是前者在测试失败后会继续执行下面的测试,而后者在测试失败后会立即终止测试。
GoogleTest中的比较断言,涉及整型,字符串, 浮点型,布尔型的比较判断
Fatal assertion | Nonfatal assertion | Verifies |
ASSERT_TRUE(condition) | EXPECT_TRUE(condition) | condition == true |
ASSERT_TRUE(condition) | EXPECT_FALSE(condition) | condition == false |
ASSERT_EQ(expected, actual) | EXPECT_EQ(expected, actual) | expected == actual |
ASSERT_NE(val1, val2) | EXPECT_NE(val1, val2) | val1 != val2 |
ASSERT_LE(val1, val2) | EXPECT_LE(val1, val2) | val1 <= val2 |
ASSERT_LT(val1, val2) | EXPECT_LT(val1, val2) | val1 < val2 |
ASSERT_GE(val1, val2) | EXPECT_GE(val1, val2) | val1 >= val2 |
ASSERT_GT(val1, val2) | EXPECT_GT(val1, val2) | val1 > val2 |
ASSERT_STREQ(expected, actual) | EXPECT_STREQ(expected, actual) | (C-string) expected == actual |
ASSERT_STRNE(s1, s2) | EXPECT_STRNE(s1, s2) | (C-string) s1 != s2 |
ASSERT_STRCASEEQ(s1, s2) | EXPECT_STRCASEEQ(s1, s2) | (C-string) s1 == s2 ignoring case |
ASSERT_STRCASENE(s1, s2) | EXPECT_STRCASENE(s1, s2) | (C-string) s1 != s2 ignoring case |
ASSERT_FLOAT_EQ(expected, actual) | EXPECT_FLOAT_EQ(expected, actual) | (float) expected == actual |
ASSERT_DOUBLE_EQ(expected, actual) | EXPECT_DOUBLE_EQ(expected, actual) | (double) expected == actual |
ASSERT_NEAR(val1, val2, abs_error) | EXPECT_NEAR(val1, val2, abs_error) | abs(val1-val2) <= abs_error |
异常断言:
Fatal assertion | Nonfatal assertion | Verifies |
ASSERT_THROW(statement, exception_type); | EXPECT_THROW(statement, exception_type); | statement throws an exception of the given type |
ASSERT_ANY_THROW(statement); | EXPECT_ANY_THROW(statement); | statement throws an exception of any type |
ASSERT_NO_THROW(statement); | EXPECT_NO_THROW(statement); | statement doesn't throw any exception |
几个特殊的断言(可用于流程控制)
SUCCEED() | 一个成功断言 |
FAIL() | 一个失败断言(fatal failure) |
ADD_FAILURE() | 一个失败断言(nonefatal failure) |
ADD_FAILURE_AT(file, line) | 在给定的文件的给定行数生成一个失败断言(nonefatal failure) |
对于一些待测试的,返回值为bool变量的函数,可以使用以下的断言(n代表参数个数):
Fatal assertion | Nonfatal assertion | Verifies |
ASSERT_PREDn(predn, val1,...); | EXPECT_PREDn(predn, val1,...); | predn(val1,...) is true |
另外,可以对测试函数进行一个包装,使用以下断言来判断包装好的函数:
Fatal assertion | Nonefatal assertion | Verifies |
ASSERT_PRED_FORMATn(pred_formatn, val1, ...); | EXPECT_PRED_FORMATn(pred_formatn, val1, ...); | pred_formatn(val1, ...) is successful |
要求pred_formatn的返回值是AssertionResult, 例子如下:
// Returns true if m and n have no common divisors except 1. bool MutuallyPrime(int m, int n) { ... } // Returns the smallest prime common divisor of m and n, // or 1 when m and n are mutually prime. int SmallestPrimeCommonDivisor(int m, int n) { ... } // A predicate-formatter for asserting that two integers are mutually prime. ::testing::AssertionResult AssertMutuallyPrime(const char* m_expr, const char* n_expr, int m, int n) { if (MutuallyPrime(m, n)) return ::testing::AssertionSuccess(); return ::testing::AssertionFailure() << m_expr << " and " << n_expr << " (" << m << " and " << n << ") are not mutually prime, " << "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n); }
使用一个AssertMutuallyPrime函数来包装一下待测试函数MutuallyPrime。此时再使用断言:
EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);
如果失败,则会打印出失败信息
b and c (4 and 10) are not mutually prime, as they have a common divisor 2.
还有类型断言:
::testing::StaticAssertTypeEq<T1, T2>();
使用这个函数可以判断T1和T2类型是否一致
最后,可以使用gmock中的各种matchers来进行断言:
Fatal assertion | Nonfatal assertion | Verifies |
ASSERT_THAT(value, matcher); | EXPECT_THAT(value, matcher); | value matches matcher |
matchers 见gmock 的介绍
参考链接:https://github.com/google/googletest/blob/master/googletest/docs/advanced.md