gtest(Google Test)使用

来源:互联网 发布:tl wn725n mac驱动 编辑:程序博客网 时间:2024/05/01 18:46

gtest 代码目录结构

说明:以 gtest-1.7.0 为例

cmake, codegear, make, msvc, xcode

构建测试项目的构建文件,如 make 就是 Makefile.

To write a test program using Google Test, you need to compile Google Test into a library and link your test with it. We provide build files for some popular build systems: msvc/ for Visual Studio, xcode/ for Mac Xcode, make/ for GNU make, codegear/ for Borland C++ Builder, and the autotools script (deprecated) and CMakeLists.txt for CMake (recommended) in the Google Test root directory.


include, src

分别为 gtest 框架实现代码的头文件和 C++ 文件。


fused-src

将上面 include 和 src 中多个 .h, .cc 文件合并成一个 .h, .cc 之后的代码。

Google Test's implementation consists of ~30 files (excluding its own tests). Sometimes you may want them to be packaged up in two files (a .h and a .cc) instead, such that you can easily copy them to a new machine and start hacking there.


samples

gtest 提供的使用例子。

https://code.google.com/p/googletest/wiki/Samples

TEST: sample1, sample2,sample4. 多个用例之间无共享代码。

TEST_F: sample3, sample5. 多个用例之间共享代码,需要继承 testing::Test.


test

gtest 的测试代码。(用 gtest 测试 gtest)


gtest 的原理及如何使用

gtest 是一个框架(其实就是库),一切测试都是通过“宏”来完成的,例如 TEST(), TEST_F(), ASSERT_TRUE() 等,基本逻辑是:

gtest 定义测试用的宏,测试代码使用宏调用被测试的函数,下面是一个完整的测试项目的组成部分:

//////////////////////////////////////////////////////////// Section 1: gtest#define TEST(...) ...//////////////////////////////////////////////////////////// Section 2: tested functionfunction(){    ...}//////////////////////////////////////////////////////////// Section 3: test case(using "macro" in gtest and// testing data to test function)TEST(function){    ...}//////////////////////////////////////////////////////////// Section 4: mainint main(int argc, char **argv){    testing::InitGoogleTest(&argc, argv);    return RUN_ALL_TESTS();}


以 samples 中的 sample1 来说:

sample1.h, sample.cc: section 2

sample1_unittest.cc: section 3

fused-src/gtest/gtest_main.cc: section 4(这部分代码也可以自己写)


编译链接流程

因此构建一个测试项目时需要编译:

1. samples/sample.cc

2. samples/sample1_unittest.cc

3. fused-src/gtest/gtest-all.cc: 实现宏的代码(库)

4. fused-src/gtest/gtest_main.cc: 主函数

注意:实际上 sample 使用的是 include 和 src 中的代码,这里为了方便说明用 fused-src 中的代码代替

编译完之后链接至 main() 即可生成可执行文件。


make/Makefile 演示了这一过程:

g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.ccg++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.ccg++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c ../src/gtest-all.ccg++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c ../src/gtest_main.ccar rv gtest_main.a gtest-all.o gtest_main.og++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest

使用方式1——源文件

按照上述方法每次编译源文件


使用方式2——库

将 fused-src/gtest/gtest-all.cc,  fused-src/gtest/gtest.h 编译成库,每次自己写 main 函数然后链接库。

因此,在项目开发中,我们仅仅需要 fused-src/gtest/gtest-all.cc,  fused-src/gtest/gtest.h 2 个文件即可。


运行过程

对于每个测试用例(TEST or TEST_F)都要经过如下流程:

1. 创建一个 test fixture(构造继承自 testing:Test 的对象)

2. 运行 SetUp()

3. 运行测试用例(TEST or TEST_F)

4. 运行 TearDown()

5. 删除  test fixture(析构继承自 testing:Test 的对象)


因为 TEST 没有继承 testing:Test 类("class GTEST_API_ Test", 抽象基类),所以具体表现是只运行了第三步。

具体流程:

https://code.google.com/p/googletest/wiki/Primer


0 0
原创粉丝点击