Google Test Frame 简单使用例子

1 序言——为什么折腾Google Test 

  被逼无奈的。

  最近研究google开源的基于列存储的数据库查询引擎supersonic源码。初略的浏览了一遍代码,竟然没有main函数,顿时惊讶的目瞪口呆呀。对于习惯了从main函数开始,一行一行跟代码的程序猿,只觉得无从下手了。看了看源码中的README,supersonic提供的是数据库查询引擎的API,不是完整的系统,无法直接运行,也就不提供main函数了,但是可以通过test/guide目录下的测试用例开始学习supersonic提供的丰富的API。supersonic的API都被封装在Google Test中进行测试,到这引出了本文的主角Google Test。

  先来看一眼Google Test的大致轮廓。

// Tests negative input.
TEST(IsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.

  EXPECT_FALSE(IsPrime(-1));
  EXPECT_FALSE(IsPrime(-2));
  EXPECT_FALSE(IsPrime(INT_MIN));
}

 

2 下载Google Test源码

  Goole Test是开源工程,可以直接在google code 上下载。下载地址 https://code.google.com/p/googletest/

 

3 多种编译方式

  Google Test是跨平台的,提供了多种编译的方式。不同的平台下的工程文件放在不同的文件夹下,在msvc目录下提供了windows下的vs工程文件,xcode目录下支持mac的工程文件,还可以通过cmake编译,也有configure编译。也可以直接使用g++编译。

  g++编译命令如下

g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
-pthread -c ${GTEST_DIR}/src/gtest-all.cc
ar -rv libgtest.a gtest-all.o

其中,GTEST_DIR 是项目的根目录。首先,将src/gtest-all.cc 编译成.o文件,然后,将其进行打包成一个静态链接库。接下来,可以加入自己写的单元测试文件。

  g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \
      -o your_test

编译完成,生成可以运行的your_test程序,现在可以运行自己写的单元测试程序了。运行实例如下:

Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (1 ms total)
[  PASSED  ] 6 tests.

 

4 通过Makefile编译

  在make目录下,有Google Test提供的Makefile文件,可以简单的修改此Makefile文件,运行我们自己写的单元测试用例。在此过程中一般需要三个文件,

sample.h            //待测试函数的头文件

sample.cc            //待测试的函数体

sample_unittest.cc        //单元测试用例

编写单元测试用例sample_unittest.cc只需要三步,

第一步,包含需要的头文件,包括自定义的待测试函数以及gtest.h

#include "sample1.h"
#include "gtest/gtest.h"

第二步,使用TEST宏定义来定义测试用例

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}

TEST宏定义有两个参数,测试用例的名字FactorialTest和测试名Positive。宏定义的函数体内部是测试的逻辑。

第三步,调用 RUN_ALL_TESTS() 执行所有的测试用例。

文件准备好之后,可以修改Makefile文件了。

GTEST_DIR = ..
USER_DIR = ../..
TESTS = merge_lists_unittest

merge_lists.o : $(USER_DIR)/merge_lists.cc $(USER_DIR)/merge_lists.h $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/merge_lists.cc

merge_lists_unittest.o : $(USER_DIR)/merge_lists_unittest.cc \
                     $(USER_DIR)/merge_lists.h $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/merge_lists_unittest.cc

merge_lists_unittest : merge_lists.o merge_lists_unittest.o gtest_main.a
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

GTEST_DIR:Google Test的工程目录

USER_DIR: 带测试的函数的文件目录

 

 

posted @ 2014-06-21 12:03  ruccsbingo  阅读(623)  评论(0编辑  收藏  举报