Gtest全称: Google C++ Testing Framework
项目链接: http://code.google.com/p/googletest/
项目链接: http://code.google.com/p/googletest/
Gtest是Google公司发布的一款非常优秀的开源C/C++单元测试框架,已被应用于多个开源项目及Google内部项目中,知名的例子包括ChromeWeb浏览器、LLVM编译器架构、ProtocolBuffers数据交换格式及工具等。至于它的优势,大家可以自己去网上搜索查看,本文主要用一个Demo描述怎么在Linux环境下使用它。
1. 下载SDK
链接:http://code.google.com/p/googletest/
我下载的版本是1.6.0
2. 解压
我解压后的位置是$HOME/bin/gtest-1.6.0
3. 编写测试用例
本例中要测试的是一个求阶乘的函数
函数头文件:func.H
#ifndef FUNC_H
#define FUNC_H
int fac(int nInput);
#endif
函数实现文件:func.C
#include "func.H"
int fac(int nInput)
{
if(nInput < 0)
{
return -1;
}
int nRev = 1;
for(int i = 1; i <= nInput; ++i)
{
nRev *= i;
}
return nRev;
}
主程序文件:主程序文件:fac_test.C