在GitHub上下载googletest
$ git clone git@github.com:google/googletest.git
进入googletest目录
编译命令如下:
$ g++ -I./include/ -I./ -c ./src/gtest-all.cc
$ ar -rv libgtest.a gtest-all.o
$ cd ./make
$ make
此时会生成一个可执行文件sample1_unittest,执行测试;
$ ./sample1_unittest
测试成功.
将静态文件拷贝到/usr/local/lib/下
$ cp libgtest.a /usr/local/lib
将头文件拷贝到/usr/local/include/下
$ cp -r gtest /usr/local/include/
写一个简单的测试文件, example1.cpp
1 #include<gtest/gtest.h>
2 #include<iostream>
3 #include<stdexcept>
4
5 int Foo(int a, int b)
6 {
7 if (a == 0 || b == 0) {
8 throw std::invalid_argument("don't do that!");
9 }
10 int c = a % b;
11 if (c == 0) {
12 return b;
13 }
14 return Foo(b, c);
15 }
16
17 TEST(FooTest, HandleNonZeroInput)
18 {
19 EXPECT_EQ(2, Foo(4, 10));
20 EXPECT_EQ(6, Foo(30, 18));
21 }
22
23 int main(int argc, char* argv[])
24 {
25 testing::InitGoogleTest(&argc, argv);
26 return RUN_ALL_TESTS();
27 }
编译命令:
$ g++ -std=c++11 example1.cpp -lgtest -pthread
// 注意需要链接的库
参考资料:
1. 如何用gtest写单元测试: http://blog.csdn.net/russell_tao/article/details/7333226
2. 初识gtest: http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html
//
posted on 2015-11-13 02:10
machinemind 阅读(409)
评论(0) 编辑 收藏 引用