GoogleTest初探(1)
2019-04-25 06:49:55来源:博客园 阅读 ()
此篇主要了解一下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
原文链接:https://www.cnblogs.com/duan-shui-liu/p/10751343.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:洛谷P4431
- 二项式反演/minmax容斥初探 2019-08-16
- Tarjan算法初探 (1):Tarjan如何求有向图的强连通分量 2019-04-11
- 整合Spring.net到asp.net网站开发中初探 2018-06-18
- jQuery学习笔记(1)-初探 2018-06-18
- Google C++单元测试框架GoogleTest---GTest的Sample1和编写 2018-06-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash