Linux c++ 测试框架 gtest使用简介
来源:互联网 发布:http协议端口号 编辑:程序博客网 时间:2024/06/04 08:25
(0)gtest是什么
gtest是一个跨平台的C++测试框架,google作品。
它支持自动发现测试,断言集,用户定义的断言,death测试,致命与非致命的失败,类型参数化测试,各类运行测试的选项和XML的测试报告。
(1)下载gtest
地址:
http://code.google.com/p/googletest/downloads/list
笔者下载的版本是1.6.0
http://googletest.googlecode.com/files/gtest-1.6.0.zip
(2)安装gtest
unzip gtest-1.6.0.zip
cd
gtest-1.6.0
.
/configure
make
确认lib目录下生成了libgtest.a和libgtest_main.a,
这两个库是后续测试程序需要链接的库。
(3)测试gtest
不妨设我们需要测试foo.h中的一个max函数(求两个数中较大的一个)
foo.h文件内如如下:
#ifndef __FOO_H__
#define __FOO_H__
int
max(
int
a,
int
b)
{
return
a>b?a:b;
}
#endif
撰写测试程序foo_test.cpp来对foo进行测试:
foo_test.cpp文件内如如下:
#include "gtest/gtest.h"
#include "foo.h"
TEST(foo, max)
{
EXPECT_EQ(2, max(2,-1));
EXPECT_EQ(3, max(2,3));
}
int
main(
int
argc,
char
** argv)
{
::testing::InitGoogleTest(&argc, argv);
return
RUN_ALL_TESTS();
}
编译foo_test.cpp,编译命令如下:
g++ -g foo_test.cpp -o foo_test \
-I.
/gtest-1
.6.0
/include
\
-L.
/gtest-1
.6.0
/lib
-lgtest -lgtest_main -lpthread
需要注意的是,要链接pthread这个库。
执行结果如下:
[==========] Running 1 test from 1 test case.
[==========] Global test environment set-up.
[==========] 1 test from foo
[ RUN ] foo.max
[ OK ] foo.max (0 ms)
[==========] 1 test from foo (0 ms total)
[==========] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
(4)简要说明
TEST(name1, name2)
{
EXPECT_EQ(value1, value2);
}
这是要执行的测试用例,后面是执行内容
name1:测试用例名称
name2:测试名称
这两个参数都只起到提示作用,我们也可以这么使用
name1:类名
name2:方法名
或者
name1:文件名
name2:函数名
EXPECT_EQ(value1, value2);
用例执行成功时,期望value1和value2是相等的,相等才算通过测试,如上例中的:
EXPECT_EQ(2, max(2,-1));
max(2, -1)的执行结果值期望是2如果不等,则用例运行失败。
int
main(
int
argc,
char
** argv)
{
::testing::InitGoogleTest(&argc, argv);
return
RUN_ALL_TESTS();
}
这个是main函数,InitGoogleTest会初始化一些环境变量,RUN_ALL_TESTS()会调用所有的TEST(name1, name2)
(6)结束语
想了解更详细的关于gtest的信息,见官网:
http://code.google.com/p/googletest
SConstruct:
env = Environment(CCFLAGS = '-g')
#gtest库
gtestFile = [ 'src/gtest-all.cc']
gmainFile = [ 'src/gtest_main.cc']
#testfile
testFile = [ 'src/foo_test.cpp' ]
includePath = [ 'include' ]
libPath = [ 'lib', '/usr/local/lib','usr/lib64' ]
libs = [ 'pthread','gtest','gtest_main']
StaticLibrary('lib/gtest',source=gtestFile,CPPPATH=includePath)
StaticLibrary('lib/gtest_main',source=gmainFile,CPPPATH=includePath)
env.Program( 'test',source=testFile,CPPPATH=includePath,LIBS=libs,LIBPATH=libPath)
- Linux c++ 测试框架 gtest使用简介
- linux下使用gtest框架进行c/c++单元测试
- Linux下Gtest测试框架应用实例
- Linux下Gtest测试框架应用实例
- 测试框架之GTest
- gtest 测试框架
- 测试框架之GTest
- gtest框架使用
- gtest框架的使用
- 基于gtest测试框架设计
- 谷歌测试框架gtest
- C++测试框架Gtest:初探
- 自动化测试框架GTest安装
- gtest简介及简单使用
- ubuntu使用gtest单元测试框架
- ubuntu使用gtest单元测试框架
- UT测试框架cxxtest和gtest对比
- 深入理解Android gtest 测试框架
- RabbitMQ
- FTP连接
- PHP连接MySQL数据库
- ActivityManager
- 正则表达式
- Linux c++ 测试框架 gtest使用简介
- webservice cxf+spring+WS-security配置示例
- HDU 1253 胜利大逃亡(三维BFS)
- proxy-target-class="true" 与proxy-target-class="false"的区别
- proc文件系统详解
- 多线程编程11-----CountDownLatch同步工具类的使用
- MyEclipse定制工具栏和菜单
- Spoj 2916 Can you answer these queries V 线段树 求任意重叠区间的最大子段和
- IOS7.0 Xcode5 AutoLayout 备忘录