操作系统:fedora15-x86_64
1、下载,解压到/home/hlf,
mv gtest-1.60 gtest
cd gtest
2、g++ -I./include -I./ -c ./src/gtest-all.cc (注意,-I后没有空格,直接加./)
3、ar -rv libgtest.a gtest-all.o
注意:
虽然在目录下有configure,甚至在make目录下有makefile文件等,但都是障眼法。
简单测试:
不妨设我们需要测试foo.h中的一个max函数(求两个数中较大的一个)
foo.h文件内如如下:
#ifndef __FOO_H__
#define __FOO_H__
intmax(inta, intb)
{
returna>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));
}
intmain(intargc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
returnRUN_ALL_TESTS();
}
编译foo_test.cpp,编译命令如下:
g++ -g foo_test.cpp -o foo_test -I/home/hlf/gtest/include -L/home/hlf/ -lgtest -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 (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (3 ms total)
[ PASSED ] 1 test.