/*************************************************************
*使用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會無法操作)
