使⽤gtest⾃动化测试并给出性能测试结果(windows版本,版
本平台也可以使⽤,但并没。。。
/*************************************************************
*使⽤gtest⾃动化测试
*
*************************************************************/
[依赖项]
eclips KEPLER
cygwin
window xp
gtest库
cmd已是管理员权限
[gtest 搭建]
a)、⾸先确定已成功安装好了eclips C++ 开发相关的⼯具,并确定g++的环境变量已能正常使⽤
b)、打开eclips,新建两个项⽬,gtest(appliction),testlibs(static library),
c)、解压gtest库,copy (include,src)到新建好的gtest⽬录中
d)、排除库⽂件的编译 gtest库中的所有⽂件,选中gtest项⽬中的include⽂件夹,右键->属性-> c/c++build -> 选中exclude resouce form build,再⽤同样的⽅法去掉src⽂件夹的编译设置
e)、设置编译头⽂件,选中gtest项⽬->右键->属性->c/c++
1)、选中setting -> 选中tool settings -> 选中 cross g++ compiler -> 选中includes ->添加include paths
"${workspace_loc:/${ProjName}/include}",
"${workspace_loc:/${ProjName}}",
"${workspace_loc:/testlibs}"
2)、点击apply,再点ok
游戏xml文件修改f)、在gtest项⽬中新增加⼀个⽂件并输⼊下⾯的代码
//-------------------------------
#include "gtest/gtest.h"
#include "src/"
#include ""
//-------------------------------
g)、选中gtest项⽬中的include⽂件夹,右键->属性->c/c++build -> 选中build steps 在post-build steps的command中输⼊"..\auto_test.bat"
h)、编写脚本
1)、向gtest项⽬中增加⼀个auto_test.bat⽂件
2)、输⼊脚本
@ > out.log
I)、在testlibs中写⼊⽬标代码(新建了⼀个class类的声明和定义)
test-class.h:
//code begin-----------------------
#ifndef TEST_CLASS_H_
#define TEST_CLASS_H_
class test_class{
public:
int write();
};
#endif /* TEST_CLASS_H_ */
/
/code  end-------------------------
<:
//code begin------------------------
#include "test-class.h"
int test_class::write(){
return 10;
}
//code end------------------------
J)、在gtest项⽬中编写测试⽤列(添加)
<:
//------------------------
#include "gtest/gtest.h"
#include <test-class.h>
TEST(TESTCASE,TEST){
test_class test;
//test.write();
EXPECT_EQ(10,test.write());
}
TEST(TESTCASE1,TEST1){
FAIL(); //⼿动增加⼀个失败的测试
}
//------------------------
k)正常编译gtest项⽬
l)、打开eclips的console,就会看到测试输出的⼀些信息,那就是测试报告console:
//------------------------
Running main() from
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from TESTCASE
[ RUN      ] TESTCASE.TEST
[      OK ] TESTCASE.TEST (0 ms)
[----------] 1 test from TESTCASE (30 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (30 ms total)
[  PASSED  ] 1 test.
[性能测试搭建]
a) 修改gtest项⽬属性
1)、选中gtest项⽬(alt+enter)->打开(c/c++ build 节点)-> setting-> cross g++ compiler
2)、修改command 原为 g++ ,改为 g++ -pg
3)、打开(cross g++ linker) -> 修改command 原为 g++ ,改为 g++ -pg
b) 修改testlibs项⽬属性
1)、选中testlibs项⽬(alt+enter)->打开(c/c++ build 节点)-> setting-> cross g++ compiler
2)、修改command 原为 g++ ,改为 g++ -pg
3)、打开(cross g++ linker) -> 修改command 原为 g++ ,改为 g++ -pg
c)、修改脚本auto_test.bat
::修改out的值就可以开启和关闭是否执⾏测试 0为开启⽤
//code begin-----------------
@set out=1
::gtest⽂件输出格式0 txt,1 xml
@set testOutType=0
::test⽇志⽂件输出⽂件名
@set log="out.log";
@if %testOutType%==1 then  ( %log%="l")
@if "%out%"==0 then goto lable1 else goto lable2
:lable1
@echo "---------start unit test----"
@echo "---------unit test results----------">log
::@ --gtest_output="xml:%log%"
@ >log
@echo "---------Performance Test Results----------">>log
@if exist "gmon.out" (goto lable3) else (goto lable4)
:lable3
@( gmon.out -b) >>log
@echo "---------test complet----------">>log
@cat log
@echo "Full output has been output to a log file."
:
:open log file
@notepad log
:lable2
@echo "">log
goto exit;
:lable4
@goto exit;
::exit script
:exit
@exit(0)
//code end-----------
d)、打开consol 窗⼝,⽣成项⽬:选中项⽬ (ctrl+b),编译操作完成后会⾃⼰打测试报告
e)、查看性能测试报告
"---------Performance Test Results----------"
Flat profile:
Each sample counts as 0.01 seconds.
%  cumulative  self              self    total
time  seconds  seconds    calls  Ts/call  Ts/call  name
19.16      1.40    1.40                            xis_data::set_data(xis_data const&)
18.96      2.77    1.38                            xis_data_array::append(xis_data)
Call graph
granularity: each sample hit covers 4 byte(s) for 0.14% of 7.28 seconds
index % time    self  children    called    name
<spontaneous>
[1]    19.2    1.40    0.00                xis_data::set_data(xis_data const&) [1]
-----------------------------------------------
<spontaneous>
[2]    19.0    1.38    0.00                xis_data_array::append(xis_data) [2]
-----------------------------------------------
<spontaneous>
"---------end Performance Test Results----------"
f)、输出报告包括(单元测试、性能测试)
console:
/
/------------------------
Running main() from
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from TESTCASE
[ RUN      ] TESTCASE.TEST
[      OK ] TESTCASE.TEST (0 ms)
[----------] 1 test from TESTCASE (30 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (30 ms total)
[  PASSED  ] 1 test.
"---------Performance Test Results----------"
Flat profile:
Each sample counts as 0.01 seconds.
%  cumulative  self              self    total
time  seconds  seconds    calls  Ts/call  Ts/call  name
19.16      1.40    1.40                            xis_data::set_data(xis_data const&) 18.96      2.77    1.38                            xis_data_array::append(xis_data) Call graph
granularity: each sample hit covers 4 byte(s) for 0.14% of 7.28 seconds index % time    self  children    called    name
<spontaneous>
[1]    19.2    1.40    0.00                xis_data::set_data(xis_data const&) [1] -----------------------------------------------
<spontaneous>
[2]    19.0    1.38    0.00                xis_data_array::append(xis_data) [2]
-----------------------------------------------
<spontaneous>
"---------end Performance Test Results----------"
g)、关闭报告,测试结束 (⼀定要操作这⼀步,否则eclips会⽆法操作)