c++的gtest示例demo搭建过程

https://www.jianshu.com/p/7f553854f9d6

补充说明

1.

c++11编辑器需要加-std=c++11

g++ -o TestAll.o -c TestAll.cpp -I./include -std=c++11

2.

apt-get install make

apt-get install g++

apt-get install build-essential

root@x::x#uname -r
4.4.0-62-generic
root@x:x# apt-get install linux-headers-'4.4.0-62-generic'

ls /usr/bin/gcc*

apt-get install gcc-5

apt-get install g++-5

3.

googletest# g++ -isystem include -I. -pthread -c src/gtest-all.cc -std=c++11

googletest#  ar -rv libgtest.a gtest-all.o

googletest# cp libgtest.a ./example/lib/

example# g++ -o functions.o -c functions.cpp

example# cp /root/cppworkspace/googletest-master/googletest/include/  ./include  >注命令写的不对,就是复制include目录,后用得其他方式复制

example# g++ -o TestAll.o -c TestAll.cpp -I./include -std=c++11
TestAll.cpp: In function ‘int main(int, char**)’:
TestAll.cpp:8:16: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
 RUN_ALL_TESTS(); //

/example# g++ -o main *.o -I./include -L./lib -lgtest -lpthread

example# ./main
[==========] Running 4 tests from 4 test suites.
[----------] Global test environment set-up.
[----------] 1 test from AddTest
[ RUN      ] AddTest.AddTestCase
[       OK ] AddTest.AddTestCase (0 ms)
[----------] 1 test from AddTest (0 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.MutilplyTestCase
[       OK ] MultiplyTest.MutilplyTestCase (0 ms)
[----------] 1 test from MultiplyTest (0 ms total)

[----------] 1 test from DivideTest
 

5.用得文件

example# ls
functions.cpp  functions.h  functions.o  functionsTest.cpp  functionsTest.o  include  lib  main  TestAll.cpp  TestAll.o

【functions.cpp】

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

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; }

==============================================

【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

【functionsTest.cpp】

#include "gtest/gtest.h"
#include "functions.h"
TEST(AddTest,AddTestCase)
{
ASSERT_EQ(2,add(1,1));
}
TEST(MinusTest,MinusTestCase)
{
ASSERT_EQ(10,myMinus(25,15));
}
TEST(MultiplyTest,MutilplyTestCase)
{
 ASSERT_EQ(12,multiply(3,4));
}
TEST(DivideTest,DivideTestCase)
{
ASSERT_EQ(2,divide(7,3));
}

【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;
}

猜你喜欢

转载自blog.csdn.net/sparrowwf/article/details/88917396