日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

测试框架之GTest

發布時間:2023/12/2 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试框架之GTest 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

gtest是google的一個C/C++測試框架,由C++實現,可在http://code.google.com/p/googletest/下載其源碼及庫文件。

gtest用法和cppunit用法差不多,個人比較習慣gtest,使用比cppunit方便些。主要通過宏TEST_F定義測試用例,通過EXPECT_系列和ASSERT_系列宏進行檢測。

1、源碼編譯:

下載gtest,解壓后,在gtest/msvc下有VC工程文件gtest.sln,用VS2005打開后生成庫文件,生成的文件在gtest/msvc/gtest/debug或gtest/msvc/gtest/release下。我們需要的是gtest.lib或gtestd.lib靜態庫文件。

2、建立一個測試工程,將gtest/include添加到頭文件路徑中。include文件夾可拷貝到工程目錄下。將gtest.lib添加到包含庫中。

3、建立主函數文件,例如main.cpp,主要用于初始化及啟動gtest測試框架:

[cpp] view plaincopy
  • #include?<iostream>??
  • ??
  • #include?"gtest.h"??
  • ??
  • GTEST_API_?int?main(int?argc,?char?**argv)?{??
  • ????std::cout?<<?"Running?main()?from?gtest_main.cc\n";??
  • ??
  • ????testing::InitGoogleTest(&argc,?argv);??
  • ????return?RUN_ALL_TESTS();??
  • }??
  • 4、將自己測試的文件及相應的頭文件包含到工程中,例如自己建立一個選擇法排序程序
    [cpp] view plaincopy
  • #include?"Sort.h"??
  • inline?void?exchange(int?*x,int?*y)??
  • {??
  • ?int?t?=?*x;??
  • ?*x?=?*y;??
  • ?*y?=?t;??
  • }??
  • void?SelectionSort(int?*pArray,int?cnt)??
  • {??
  • ????if(cnt?<=?1)??
  • ????????return;??
  • ????int?i?=?0;??
  • ????int?j?=?0;??
  • ????for(i=0;i<cnt;i++)??
  • ????{??
  • ????????for(j=i+1;j<cnt;j++)??
  • ????????{??
  • ????????????if(pArray[i]?>?pArray[j])??
  • ????????????????exchange(pArray+i,pArray+j);??
  • ????????}??
  • ????}??
  • }??
  • 5、建立測試文件,比如叫做SortTest.cpp,建立測試類,并聲明需測試的函數如下:
    [cpp] view plaincopy
  • #include?"stdio.h"??
  • #include?"gtest.h"??
  • #include?"Sort.h"??
  • ??
  • using?testing::Test;??
  • ??
  • class?Test_Sort?:?public?Test??
  • {??
  • ????void?SetUp()??
  • ????{??
  • ??
  • ????}??
  • ??
  • ????void?TearDown()??
  • ????{??
  • ??
  • ????}??
  • };??
  • 一個Test_Sort可以添加多個測試用例。其中Setup函數是每個測試用例執行前都要執行的函數,可以用于統一的初始化。TearDown相反是最后調用的函數,可用戶Setup中資源的釋放。

    本例中不使用。

    6、添加自己的測試用例:

    [cpp] view plaincopy
  • TEST_F(Test_Sort,SelectionSortTest)??
  • {??
  • ????const?unsigned?int?cnt?=?10;??
  • ????int?test1[]?=?{10,9,8,7,6,5,4,3,2,1};??
  • ????int?stest1[]?=?{1,2,3,4,5,6,7,8,9,10};??
  • ????int?i?=?0;??
  • ??
  • ????printf("SelectionSort?--?TestCase:");??
  • ????SelectionSort(test1,cnt);??
  • ????for(i=0;i<cnt;i++)??
  • ????{??
  • ????????ASSERT_EQ(test1[i],stest1[i]);??
  • ????}??
  • ????printf("----OK\n");??
  • }??
  • 7、運行后有如下提示:

    可以看到執行成功。

    8、如果失敗,提示如下:

    可以看到錯誤的位置,及錯誤的值、期望值。


    最簡單的gtest使用工程就這樣完成了。

    常用的判斷宏主要有:

    ASSERT_TRUE(A):判斷A是否true,如果為false則assert,不繼續執行。

    ASSERT_EQ(A,B):判斷AB是否相等,如果不相等則提示,不繼續執行。

    EXPECT_TRUE(A):判斷A是否true,如果為false則提示,繼續執行。

    EXPECT_EQ(A,B):判斷AB是否相等,如果不相等則提示,繼續執行。

    一般也就用這么4個。其他的比如比較可以轉化為ASSERT_TRUE,但這種情況下,用例失敗時的提示信息不如ASSERT_LT等多罷了。


    為了方便測試排序類算法,我將排序的用例寫在了一個函數中,對調試有所不便,但避免了每個排序函數都寫相同的用例:

    源碼如下:

    [cpp] view plaincopy
  • #include?"stdio.h"??
  • #include?"gtest.h"??
  • #include?"Common.h"??
  • #include?"Sort.h"??
  • ??
  • using?testing::Test;??
  • ??
  • class?Test_Sort?:?public?Test??
  • {??
  • ????void?SetUp()??
  • ????{??
  • ??
  • ????}??
  • ??
  • ????void?TearDown()??
  • ????{??
  • ??
  • ????}??
  • };??
  • ??
  • bool?checkSorted(int?*pArray,int?cnt)??
  • {??
  • ????if(cnt?<=?0)??
  • ????????return?true;??
  • ????int?i?=?0;??
  • ????for(i=0;i<cnt-1;i++)??
  • ????????if(pArray[i+1]?<?pArray[i])??
  • ????????????return?false;??
  • ????return?true;??
  • }??
  • ??
  • bool?cmpArray(int?*pArray,int?*pSorted,int?cnt)??
  • {??
  • ????if(cnt?<=?0)??
  • ????????return?true;??
  • ????int?i?=?0;??
  • ????for(i=0;i<cnt;i++)??
  • ????????if(pArray[i]?!=?pSorted[i])??
  • ????????????return?false;??
  • ????return?true;??
  • }??
  • ??
  • void?PrintArray(int?*pArray,int?cnt)??
  • {??
  • ????printf("\n");??
  • ????int?i?=?0;??
  • ????for(i=0;i<cnt;i++)??
  • ????????printf("%d,",pArray[i]);??
  • ????printf("\n");??
  • }??
  • ??
  • void?SortTest(SK_SORT?pFunc,const?char?*strFuncName)??
  • {??
  • ????static?const?int?bigArray?=?1024;??
  • ????static?const?int?testCnt?=?9;??
  • ????static?int?test1[]?=?{1,2,3,4,5,6,7,8,9,10};??
  • ????static?int?stest1[]?=?{1,2,3,4,5,6,7,8,9,10};??
  • ????static?int?test2[]?=?{10,9,8,7,6,5,4,3,2,1};??
  • ????static?int?stest2[]?=?{1,2,3,4,5,6,7,8,9,10};??
  • ????static?int?test3[]?=?{1};??
  • ????static?int?stest3[]?=?{1};??
  • ????static?int?test4[]?=?{1,1,1,1,1};??
  • ????static?int?stest4[]?=?{1,1,1,1,1};??
  • ????static?int?test5[]?=?{1,2,3,2,1};??
  • ????static?int?stest5[]?=?{1,1,2,2,3};??
  • ????static?int?test6[]?=?{3,1,4,6,5,9,7,5,1};??
  • ????static?int?stest6[]?=?{1,1,3,4,5,5,6,7,9};??
  • ????static?int?test7[]?=?{4,2,7,4,5,6,9,4,3,1,5,7,9,32,4,5,7,8};??
  • ????static?int?stest7[]?=?{1,2,3,4,4,4,4,5,5,5,6,7,7,7,8,9,9,32};??
  • ????static?int?test8[bigArray];??
  • ????static?int?stest8[bigArray];??
  • ????static?int?test9[bigArray];??
  • ????static?int?stest9[bigArray];??
  • ????static?bool?binit?=?false;??
  • ????int?i?=?0;??
  • ????if(!binit)??
  • ????{??
  • ????????binit?=?true;??
  • ????????for(i=0;i<bigArray;i++)??
  • ????????{??
  • ????????????test8[i]?=?bigArray-i;??
  • ????????????stest8[i]?=?i+1;??
  • ????????}??
  • ????????for(i=0;i<bigArray;i++)??
  • ????????{??
  • ????????????test9[i]?=?get_random(0,32767);??
  • ????????????stest9[i]?=?test9[i];??
  • ????????}??
  • ????????SelectionSort(stest9,bigArray);??
  • ????}??
  • ??
  • ????struct?tagTestCase??
  • ????{??
  • ????????int?*pArray;??
  • ????????int?*pSorted;??
  • ????????int?cnt;??
  • ????????bool?bPrint;??
  • ????}tests[testCnt]?=?{{test1,stest1,10,false},??
  • ????????????????{test2,stest2,10,false},??
  • ????????????????{test3,stest3,1,false},??
  • ????????????????{test4,stest4,5,false},??
  • ????????????????{test5,stest5,5,false},??
  • ????????????????{test6,stest6,9,false},??
  • ????????????????{test7,stest7,18,false},??
  • ????????????????{test8,stest8,bigArray,false},??
  • ????????????????{test9,stest9,bigArray,false},};??
  • ??????
  • ????printf("%s?--?TestCase:",strFuncName);??
  • ????for(i=0;i<testCnt;i++)??
  • ????{??
  • ????????printf("%d..",i);??
  • ??????????
  • ????????if(tests[i].bPrint)??
  • ????????????PrintArray(tests[i].pArray,tests[i].cnt);??
  • ??????????
  • ????????pFunc(tests[i].pArray,tests[i].cnt);??
  • ??????????
  • ????????if(tests[i].bPrint)??
  • ????????????PrintArray(tests[i].pArray,tests[i].cnt);??
  • ??
  • ????????ASSERT_TRUE(cmpArray(tests[i].pArray,tests[i].pSorted,tests[i].cnt));??
  • ????}??
  • ????printf("----OK\n");??
  • }??
  • ??
  • TEST_F(Test_Sort,SelectionSort)??
  • {??
  • ????SortTest(SelectionSort,"SelectionSort");??
  • }??
  • ??
  • 下節開始算法導論的學習,其中排序算法的測試用例就用上面。?
  • 總結

    以上是生活随笔為你收集整理的测试框架之GTest的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。