胡漾

\$cd gtest-svn

\$g++ -I./include -I./ -c ./src/gtest-all.cc

\$ ar -rv libgtest.a gtest-all.o

//functions.h #ifndef _FUNCTIONS_H #define _FUNCTIONS_H

int add(int one,int two); int myMinus(int one,int two); int multiply(int one,int two); int divide(int one,int two); #endif //functions.cpp #include "functions.h" int add(int one,int two){ return one+two; } int myMinus(int one,int two){ return one-two; } int multiply(int one,int two){ return one*two; } int divide(int one,int two){ return one/two; } 四、编写单元测试代码 1、编写单元测试代码functionsTest.cpp

//functionsTest.cpp #include "gtest/gtest.h" #include "functions.h"

//TestAll.cpp #include "gtest/gtest.h" #include <iostream> using namespace std;

int main(int argc,char* argv[]) { //testing::GTEST_FLAG(output) = "xml:"; //若要生成xml结果文件 testing::InitGoogleTest(&argc,argv); //初始化 RUN_ALL_TESTS(); //跑单元测试 return 0; } 五、编译与运行测试 1、编译

1. 复制gtest库文件

\$mkdir lib

\$cp <the path>/libgtest.a lib

1. 复制gtest头文件

gtest1.6.0目录下的include包含了需使用到的头文件。把include复制到GTestApp中来。

3）编译和链接

\$ g++ -o functions.o -c functions.cpp

\$ g++ -o functionsTest.o -c funciontsTest.cpp -I./include

\$ g++ -o TestAll.o -c TestAll.cpp -I./include

2、运行与测试

\$./main

[==========] Running 4 tests from 4 test cases. [----------] Global test environment set-up. [----------] 1 test from AddTest [ RUN ] AddTest.AddTestCase [ OK ] AddTest.AddTestCase (0 ms) [----------] 1 test from AddTest (1 ms total)

[----------] 1 test from MinusTest [ RUN ] MinusTest.MinusTestCase [ OK ] MinusTest.MinusTestCase (0 ms) [----------] 1 test from MinusTest (0 ms total)

[----------] 1 test from MultiplyTest [ RUN ] MultiplyTest.MultiplyTestCase [ OK ] MultiplyTest.MultiplyTestCase (0 ms) [----------] 1 test from MultiplyTest (1 ms total)

[----------] 1 test from DivideTest [ RUN ] DivideTest.DivideTestCase [ OK ] DivideTest.DivideTestCase (0 ms) [----------] 1 test from DivideTest (3 ms total)

[----------] Global test environment tear-down [==========] 4 tests from 4 test cases ran. (8 ms total) [ PASSED ] 4 tests.

testing::GTEST_FLAG(output) = "xml:";

[==========] Running 4 tests from 4 test cases. [----------] Global test environment set-up. [----------] 1 test from AddTest [ RUN ] AddTest.AddTestCase [ OK ] AddTest.AddTestCase (0 ms) [----------] 1 test from AddTest (1 ms total)

[----------] 1 test from MinusTest [ RUN ] MinusTest.MinusTestCase [ OK ] MinusTest.MinusTestCase (0 ms) [----------] 1 test from MinusTest (1 ms total)

[----------] 1 test from MultiplyTest [ RUN ] MultiplyTest.MultiplyTestCase [ OK ] MultiplyTest.MultiplyTestCase (0 ms) [----------] 1 test from MultiplyTest (1 ms total)

[----------] 1 test from DivideTest [ RUN ] DivideTest.DivideTestCase [ OK ] DivideTest.DivideTestCase (0 ms) [----------] 1 test from DivideTest (0 ms total)

[----------] Global test environment tear-down [==========] 4 tests from 4 test cases ran. (4 ms total) [ PASSED ] 4 tests.

http://graybull.is-programmer.com/posts/37871.html

http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html

胡漾

2012/03/09
274
0

2012/03/09
461
0
C++单元测试框架：gtest

2014/03/31
3.3K
0

2012/03/09
693
0

2012/03/09
638
0

Taro 兼容 h5 踩坑指南

dkvirus

4
0
Spring boot 静态资源访问

0. 两个配置 spring.mvc.static-path-patternspring.resources.static-locations 1. application中需要先行的两个配置项 1.1 spring.mvc.static-path-pattern 这个配置项是告诉springboo......

moon888

3
0
hash slot（虚拟桶）

4
0
Kafka 原理和实战

vivo互联网技术

19
0
java数据类型

audience_1

9
0