跨平台C++单元测试框架GTest -- Linux下试用 - graybull's Blog
跨平台C++单元测试框架GTest -- Linux下试用
GTest是Google开发的跨平台而且开源的C++单元测试框架,很好很强大。首先奉上下载地址:https://code.google.com/p/googletest/ 。关于GTest在Windows下使用,CoderZh给出了十分详尽的使用指南:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 。
这里,是我在Linux下初尝GTest 1.6.0。
- 生成gtest库
- 编写简单功能函数
- 编写单元测试代码
- 编译与运行测试
生成gtest的静态库
首先下载gtest源代码包,然后解压。
编译生成gtest-all.o文件(注意-I后无空格):
$g++ -I./include -I./ -c ./src/gtest-all.cc
再生成.a静态库文件:
$ ar -rv libgtest.a gtest-all.o
若成功,会在当前目录下生成libgtest.a库。可拷贝它到C++单元测试项目中去,以便使用。
编写简单功能函数
新建一个项目(目录)GTestApp,包含两个代码文件: functions.h, functions.cpp。实现两个int变量的加减乘除功能。
//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; }
编写单元测试代码
单元测试的内容写在functionsTest.cpp中:
//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;
}
编译与运行测试
1) 复制gtest库文件
在GTestApp目录下新建lib目录,并复制libgtest.a到其中。
$mkdir lib
$cp <the path>/libgtest.a lib
2) 复制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
链接:
$ g++ -o main *.o -I./include -L./lib -lgtest -lpthread #注意不是-libgtest,同时需要用到 libpthread这个库
方法二,用我的万能Makefile,请看 http://graybull.is-programmer.com/posts/37758.html
只需对Makefile作以下修改:
EXECUTABLE := main
LIBDIR:= ./lib
LIBS := gtest pthread
INCLUDES:= ./include
然后,运行$ make 即可生成可执行文件main。
4)运行测试
$./main
若要生成xml结果文件,也可添加运行选项:
$ ./main --gtest_output=xml
输出结果:
<div>[jackie@localhost GTestApp]$ ./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 (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
[ 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. (0 ms total)
[ PASSED ] 4 tests.
[jackie@localhost GTestApp]$ ./main -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 (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
[ 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. (0 ms total)
[ PASSED ] 4 tests.
</div>
2019年7月17日 23:07
Thank you so much! I hope to hear more updates from you gmail email login
2021年7月20日 01:32
You are travel lovers? Where will you go for the next destination? Let Things to do post show you their favorite places with their beautiful photos.
2022年8月17日 22:51
UP Board 12th Question Paper 2023, The Board of Higher Education Uttar Pradesh UPMSP, conducts the final Matriculation examination every year. UP Intermediate Question Paper 2022 PDF This year too, Uttar-Pradesh Higher Secondary School Certificate public Examination 2023 will be performed during March and April 2023.
2024年1月08日 18:10
This is very interesting.
2024年1月11日 06:45
JNANABHUMI AP provides all the latest educational updates and many more. The main concept or our aim behind this website has been the will to provide resources with full information on each topic jnanabhumiap.in which can be accessed through the Internet. To ensure that every reader gets what is important and worthy about the topic they search and link to hear from us.