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(-));
EXPECT_FALSE(IsPrime(-));
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 tests from test cases.
[----------] Global test environment set-up.
[----------] tests from FactorialTest
[ RUN ] FactorialTest.Negative
[ OK ] FactorialTest.Negative ( ms)
[ RUN ] FactorialTest.Zero
[ OK ] FactorialTest.Zero ( ms)
[ RUN ] FactorialTest.Positive
[ OK ] FactorialTest.Positive ( ms)
[----------] tests from FactorialTest ( ms total) [----------] tests from IsPrimeTest
[ RUN ] IsPrimeTest.Negative
[ OK ] IsPrimeTest.Negative ( ms)
[ RUN ] IsPrimeTest.Trivial
[ OK ] IsPrimeTest.Trivial ( ms)
[ RUN ] IsPrimeTest.Positive
[ OK ] IsPrimeTest.Positive ( ms)
[----------] tests from IsPrimeTest ( ms total) [----------] Global test environment tear-down
[==========] tests from test cases ran. ( ms total)
[ PASSED ] 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(, Factorial());
EXPECT_EQ(, Factorial());
EXPECT_EQ(, Factorial());
EXPECT_EQ(, Factorial());
}

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: 带测试的函数的文件目录

最新文章

  1. iOS中的一些细节
  2. 通过ios实现RSA加密和解密
  3. ecshop 调用收货地址
  4. Python基础1-Python环境搭建
  5. Asp.Net customErrors与httpErrors的区别
  6. SSH自定义分页标签
  7. Codeforces Round #365 (Div. 2) A 水
  8. oracle portlist.ini
  9. apache启动报错(98)Address already in use: make_sock: could not bind to...
  10. HDU-4651 Partition 整数拆分,递推
  11. python学习笔记--Django入门四 管理站点--二
  12. mysql报错"ERROR 1206 (HY000): The total number of locks exceeds the lock table size"的解决方法
  13. 高性能双端js模板---simplite
  14. MyEclipse和Eclipse非常方便的快捷键
  15. PHP中public,private,protected,abstract等关键字用法详解
  16. 2017ecjtu-summer training #4 UESTC 1599
  17. .NET Core快速入门教程 3、我的第一个.NET Core App (CentOS篇)
  18. Java基础学习笔记十二 类、抽象类、接口作为方法参数和返回值以及常用API
  19. Centos7 nginx 虚拟主机、反向代理服务器及负载均衡,多台主机分离php-fpm实验,之强化篇,部署zabbix为例
  20. Spark学习之路 (一)Spark初识

热门文章

  1. HW3.21
  2. POJ2192 - Zipper(区间DP)
  3. 8-12-COMPETITION
  4. Robotium学习笔记二
  5. Java比较运算符
  6. android 中view的绘制过程
  7. 【转】C++中继承中的同名成员问题
  8. [Javascript] Drawing Paths - Curves and Arcs
  9. 69 Spring Interview Questions and Answers – The ULTIMATE List--reference
  10. linux 定时任务计划