(Android下使用)Google Test C++单元测试框架(一)

什么是gtest

gtest是一个跨平台的(Liunx、Mac OS X、Windows、Cygwin、Windows CE and Symbian)C++单元测试框架,由google公司发布。gtest是为在不同平台上为编写C++测试而生成的。它提供了丰富的断言、致命和非致命判断、参数化、”死亡测试”等等。

官网:GoogleTest

它分为好几种测试工具。依次介绍:

GTest Runner

GTest Runner is a Qt5 based automated test-runner and Graphical User Interface with powerful features for Windows and Linux platforms.

GTest Runner是基于qt5的自动测试运行程序和图形用户界面,具有Windows和Linux平台的强大功能。

Google Test UI

Google Test UI is test runner that runs your test binary, allows you to track its progress via a progress bar, and displays a list of test failures. Clicking on one shows failure text. Google Test UI is written in C#.

Google Test UI是运行测试程序的测试运行程序,允许您通过进度条跟踪其进度,并显示测试失败的列表。单击其中一个显示故障文本。谷歌测试用户界面是用C#语言编写的。

GTest TAP Listener

GTest TAP Listener is an event listener for Google Test that implements the TAP protocol for test result output. If your test runner understands TAP, you may find it useful.

gtest-tap-listener是Google测试的事件侦听器,它实现了测试结果输出的tap协议。如果您的测试人员理解TAP协议,您可能会发现它很有用。

gtest-parallel

gtest-parallel is a test runner that runs tests from your binary in parallel to provide significant speed-up.

gtest-parallel是一个测试运行程序,它并行运行可执行程序中的测试,以提供显著的加速。

oogleTest Adapter

GoogleTest Adapter is a VS Code extension allowing to view Google Tests in a tree view, and run/debug your tests.

GoogleTest Adapter是一个允许在树视图中查看Google测试并运行/调试测试的vs代码扩展。

如何使用

Exercise a particular program path with specific input values and verify the results。

使用特定的输入值运行特定的程序路径并验证结果。

听起来比较绕口,其实就是一个叫做测试单元的概念。

先来解释下test case:
A set of preconditions, inputs, actions (where applicable), expected results and postconditions, developed based on test conditions.

基于测试条件开发的一组先决条件、输入、动作(如适用)、预期结果和后置条件。

在gtest中的使用就是一个函数:

TEST()

Simple Tests

To create a test:

  1. Use the TEST() macro to define and name a test function, These are ordinary C++ functions that don’t return a value.

使用TEST()宏定义和命名测试函数,这些是不返回值的普通C++函数。

  1. In this function, along with any valid C++ statements you want to include, use the various googletest assertions to check values.

在这个函数中,连同任何要包含的有效C++语句,使用各种googletest assertions 来检查值。

  1. The test’s result is determined by the assertions; if any assertion in the test fails (either fatally or non-fatally), or if the test crashes, the entire test fails. Otherwise, it succeeds.

测试结果由断言确定;如果测试中的任何断言失败(致命或非致命),或者如果测试崩溃,则整个测试都失败。否则,它会成功。

断言(assertions)

gtest的使用离不开断言。什么是断言?

Google Test断言是类似于函数调用的宏。您可以通过对其行为进行断言来测试类或函数。当断言失败时,Google Test会打印断言的源文件和行号位置以及失败消息。

gtest中断言的宏可以分为两类:一类是ASSERT宏,另一类就是EXPECT宏了。
1 ASSERT_系列:如果当前点检测失败则退出当前函数
2 EXPECT_系列:如果当前点检测失败则继续往下执行

bool值检查

ASSERT_ EXPECT_ Verifies
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition is true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition is false

数值型

ASSERT_ EXPECT_ Verifies
ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
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

在发生故障时,Google测试同时打印val1和val2。

而且值参数通过断言的比较运算符必须可以比较,否则会出现编译错误。

字符串

ASSERT_ Verifies
ASSERT_STREQ(expected_str,actual_str); the two C strings have the same content
ASSERT_STRNE(str1, str2); the two C strings have different content
ASSERT_STRCASEEQ(expected_str,actual_str); the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1, str2); the two C strings have different content, ignoring case
EXPECT_ Verifies
EXPECT_STREQ(expected_str,actual_str); the two C strings have the same content
EXPECT_STRNE(str1, str2); the two C strings have different content
EXPECT_STRCASEEQ(expected_str,actual_str); the two C strings have the same content, ignoring case
EXPECT_STRCASENE(str1, str2); the two C strings have different content, ignoring case

注:断言名中的“CASE”表示忽略大小写。
而且,NULL指针和空字符串被认为是不同的。

异常检查

ASSERT_ Verifies
ASSERT_THROW(statement, exception_type); statement throws an exception of the given type
ASSERT_ANY_THROW(statement); statement throws an exception of any type
ASSERT_NO_THROW(statement); statement doesn’t throw any exception
EXPECT_ Verifies
EXPECT_THROW(statement, exception_type); statement throws an exception of the given type
EXPECT_ANY_THROW(statement); statement throws an exception of any type
EXPECT_NO_THROW(statement);	|statement doesn't throw any exception

测试用例

伪码:

TEST(TestSuiteName, TestName) {
  ... test body ...
}

TEST() arguments go from general to specific. The first argument is the name of the test case, and the second argument is the test’s name within the test case. Both names must be valid C++ identifiers, and they should not contain underscore (_). A test’s full name consists of its containing test case and its individual name. Tests from different test cases can have the same individual name.

TEST() 参数从常规变为特定。第一个参数是测试用例的名称,第二个参数是测试用例中的测试名称。

两个名称必须是有效的C++标识符,并且它们不应该包含下划线。

测试的全名由它的包含测试用例和它的单个名称组成。来自不同测试用例的测试可以具有相同的单个名称。

例子:

int  add_sum(int a, int b)
{
    return a + b;
}

TEST(addsumTest, OneAddZeroInput) {
  EXPECT_EQ(add_sum(1,0), 1); 
}

TEST(addsumTest, addSomeInput) {
  EXPECT_EQ(add_sum(1, 0), 1); 
  EXPECT_EQ(add_sum(2, 0), 2); 
  EXPECT_EQ(add_sum(3, 3), 6); 
  EXPECT_EQ(add_sum(8, 1024), 40320);
}

放张截图:O(∩_∩)O哈哈~

GoogleTest按测试用例对测试结果进行分组,因此逻辑上相关的测试应该在同一个测试用例中;换句话说,它们的TEST()的第一个参数应该相同。

在上面的例子中,我们有两个测试,OneAddZeroInput和addSomeInput,它们属于同一个测试用例addsumTest。

今天就描述到这里。明天继续。O(∩_∩)O哈哈~

参考

Google C++单元测试框架—Gtest框架简介(译文)

你可能感兴趣的:(Android学习笔记)