zoukankan      html  css  js  c++  java
  • 使用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会无法操作)
  • 相关阅读:
    方法
    属性
    Cocoapods完整使用篇
    这样好用的ReactiveCocoa,根本停不下来【转载】
    OS开发之旅之App的生命周期【转载】
    ios 推送证书没有密钥 解决方案【转载】
    移动应用开发测试工具Bugtags集成和使用教程【转载】
    MagicalRecord使用教程【转载】
    GIT客户端的使用【原创】
    iOS开发系列--让你的应用“动”起来【转载】
  • 原文地址:https://www.cnblogs.com/czjone/p/3309720.html
Copyright © 2011-2022 走看看