Googletest入门

读了Googletest Primer ,稍微记录一下。

一些特性

  • 测试是独立和可重复的,当一个测试失败的时候,Googletest可以让你隔离运行它
  • googletest将相关测试分组到可以共享数据和子例程的测试套件中。
  • 跨平台,跨编译器,在多个配置下工作
  • googletest在一次失败后会继续执行。并且可以让非致命错误的测试在报告错误后继续运行
  • Googletest让用户关注测试内容本身,自动跟踪所有定义的测试,并且不需要用户为了运行它们而枚举它们。
  • 性能快

断言

文档里面有这样的一段话:

The assertions come in pairs that test the same thing but have different effects on the current function. ASSERT_* versions generate fatal failures when they fail, and abort the current function. EXPECT_* versions generate nonfatal failures, which don’t abort the current function. Usually EXPECT_* are preferred, as they allow more than one failure to be reported in a test. However, you should use ASSERT_* if it doesn’t make sense to continue when the assertion in question fails.

感觉大概就是ASSERT_*的断言如果失败的时候抛出致命错误,而EXPECT_*则抛出非致命错误。抛出致命错误的时候就会直接中止当前测试,而非致命错误则会继续测试。

TEST

TEST宏有两个参数:

1
2
3
TEST(TestSuiteName, TestName) {
... test body ...
}

从我的主观感觉来说,TestSuite是一组测试,而Test则是某个测试。根据文档我稍微写了一点代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// test1.cpp
#include <iostream>
#include <gtest/gtest.h>

int Factorial(int x) {
int res = 1;
for(int i = 1;i <= x;i++){
res *= i;
}
return res;
}

TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}

TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(8), 40320);
}

TEST(FactorialTest1, HandlesPositiveInput1) {
EXPECT_EQ(Factorial(3), 6);
EXPECT_EQ(Factorial(8), 40320);
}

int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译并运行:

1
2
g++ -o test1 test1.cpp -lgtest_main -lgtest -lpthread -std=c++11
./test1

结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ./test1 
[==========] Running 3 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from FactorialTest
[ RUN ] FactorialTest.HandlesZeroInput
[ OK ] FactorialTest.HandlesZeroInput (0 ms)
[ RUN ] FactorialTest.HandlesPositiveInput
[ OK ] FactorialTest.HandlesPositiveInput (0 ms)
[----------] 2 tests from FactorialTest (0 ms total)

[----------] 1 test from FactorialTest1
[ RUN ] FactorialTest1.HandlesPositiveInput1
[ OK ] FactorialTest1.HandlesPositiveInput1 (0 ms)
[----------] 1 test from FactorialTest1 (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 2 test suites ran. (0 ms total)
[ PASSED ] 3 tests.

TEST_F

在这里我看到了使用 Google Test 测试框架,跑了一下作者的代码,对文档中所说的跑多个测试作用于一组数据有了一定的理解。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// test2.cpp
#include <gtest/gtest.h>
#include <vector>
#include <iostream>

class VectorTest : public testing::Test
{
protected:
virtual void SetUp() override
{
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
}
std::vector<int> vec;
};
// 注意这里使用 TEST_F,而不是 TEST
TEST_F(VectorTest, PushBack)
{
// 虽然这里修改了 vec,但对其它测试函数来说是不可见的
vec.push_back(4);
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec.back(), 4);
}
TEST_F(VectorTest, Size)
{
EXPECT_EQ(vec.size(), 3);
}
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
1
g++ -o test2 test2.cpp -lgtest_main -lgtest -lpthread -std=c++11./test1

TEST_F的第一个参数是Test Fixtures的名字,在C++里面是::testing:: Test类派生的类。第二个参数是TestName。而在test2中两个测试里面共享的数据成员vec是隔离的,所以就可以比较方便的进行测试。

main函数

在文档中也提到,::testing::InitGoogleTest()这个函数根据传递的参数来做一些操作,不过这个暂时我也用不到,后面学的更加深入了再来补充。