关于 Google Test(有时也称为gtest)的介绍,会分为两篇文章。本文主要介绍 google test 的相关知识,另外一篇则会演示一些示例程序。

1 Why googletest
此处引用 google test 在 GitHub 上的介绍:

googletest helps you write better C++ tests.

googletest is a testing framework developed by the Testing Technology team with Google's specific requirements and constraints in mind. No matter whether you work on Linux, Windows, or a Mac, if you write C++ code, googletest can help you. And it supports any kind of tests, not just unit tests.

2 相关知识
2.1 Test、Test Case 和 Test Suite
此处介绍三个概念:Test、Test Case 和 Test Suite。

这三者在某些场景下容易混淆。

由于某些历史原因,googletest 使用 Test Case 来将相关的 Test 归为一组,然而,当前 ISTQB(International Software Testing Qualifications Board) 和很多关于软件质量书籍都使用 Test Suite 替换 Test Case;而 googletest 中的 Test 则对应 ISTQB 的 Test Case。总结后,即下表内容:

Meaning googletest Term ISTQB Term
Exercise a particular program path with specific input values and verify the results TEST() Test Case
A set of several tests related to one component Test Case Test Suite
2.2 基本概念
使用 googletest,最先写的就是断言(assertion)。断言是一种检查某个条件是否为真的描述。断言的结果可以是成功、非致命失败、致命失败。当致命失败发生时,当前函数将会终止;而断言的其他结果则不会有此效果。

Test 使用断言来判断测试代码的行为:如果一个 Test 崩溃了或者出现了一个失败的断言,则该 Test 就失败了;反之,它就是成功的。

Test case 包括一个或多个 Test。我们应当把 Test 打包、分组,放入 Test Case 中,以便测试代码的结构更加清晰。当一个 Test Case 中的多个 Test 需要共享对象和子程序时,我们可以把这些共享内容放入一个(test fixture)类中。

一个测试程序可以包含多个 Test Case。

2.3 断言(assertion)
googletest 的断言是类似函数调用的宏。

我们可以通过编写相关的断言,来测试类或函数的行为。如果断言失败了,googletest 将打印该断言的源文件及行号信息,以及该失败信息。我们也可以定制 googletest 的失败信息。

当前,有两种断言可供我们使用:

ASSERT_* :当断言失败时,产生致命错误,并终止当前函数;
EXPECT_* :当断言失败时,产生非致命错误,并且不会终止当前函数。
通常,我们都会选择 EXPECT_*,因为它能让我们在一次测试中测试出更多的失败情况。不过,如果我们想要在出现失败的测试时立即终止程序,则要选择 ASSERT_*。

注意:因为 ASSERT_* 会在失败时立即终止函数,那么就可能跳过后面程序中进行清理工作的代码,由此可能会产生内存泄露。所以我们在使用 ASSERT_* 时,要留心检查堆内存,防止内存泄露。

一些常见的断言语句如下:

【基本断言】:

Fatal assertion Nonfatal assertion Verifies
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false
【二元断言】:

Fatal assertion Nonfatal assertion Verifies
ASSERT_EQ(val1, val2); EXPECT_EQ(val1, val2); val1 == val2
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2
【字符串断言】:

Fatal assertion Nonfatal assertion Verifies
ASSERT_STREQ(str1, str2); EXPECT_STREQ(str1, str2); the two C strings have the same content
ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1, str2); the two C strings have different contents
ASSERT_STRCASEEQ(str1, str2); EXPECT_STRCASEEQ(str1, str2); the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1, str2); EXPECT_STRCASENE(str1, str2); the two C strings have different contents, ignoring case
关于 googletest 的用法示例,请点击此处。
————————————————
版权声明:本文为CSDN博主「liitdar」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liitdar/article/details/85712973

posted on 2021-06-09 17:57  bdshr  阅读(422)  评论(0编辑  收藏  举报