使用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
 
 f)、在gtest项目中新增加一个main.cc文件并输入下面的代码
 
//-------------------------------
#include "gtest/gtest.h"
#include "src/gtest_main.cc"
#include "src/gtest-all.cc"
//-------------------------------
 
 g)、选中gtest项目中的include文件夹,右键->属性->c/c++build -> 选中build steps 在post-build steps的command中输入"..\auto_test.bat"
 
h)、编写脚本
 
1)、向gtest项目中增加一个auto_test.bat文件
 
2)、输入脚本
 
@gtest.exe > 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-------------------------
 
test-class.cc:
//code begin------------------------
#include "test-class.h"
 
int test_class::write(){
 return 10;
}
//code end------------------------
 
 J)、在gtest项目中编写测试用列(添加test.cc)
 
test.cc:
//------------------------
#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 gtest_main.cc
[==========] 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%="out.xml")
 
@if "%out%"==0 then goto lable1 else goto lable2 
 
:lable1
@echo "---------start unit test----"
@echo "---------unit test results----------">log
::@gtest.exe --gtest_output="xml:%log%"
@gtest.exe >log
@echo "---------Performance Test Results----------">>log
@if exist "gmon.out" (goto lable3) else (goto lable4)
:lable3
@(gprof gtest.exe 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 gtest_main.cc
[==========] 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会无法操作)

转载于:https://www.cnblogs.com/czjone/p/3309720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值