qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Gtest 單元測試初試

           下載,安裝,省略,直接上demo源碼
            ===========linux運行gtest=================
          ----------------code.cpp-------------------
          #include "code.h"
          int f( int a ){    return a+1;}
          int b( int b ){    return b+2;}
          ----------------code_test.cpp-------------------
          #include "code.h"
          #include <gtest/gtest.h>
          TEST( TEST_A,NAME ){//TEST_A 是測試用例名稱, NAME 是測試名稱
          EXPECT_EQ( 2,f( 1 ) );//這個測試執行函數f,參數為1,查看返回值是不是2
          EXPECT_EQ( 3,f( 2 ) );
          }
          TEST( TEST_B,NAME ){
          EXPECT_EQ( 4,b( 2 ) );
          }
          ----------------main_test.cpp-------------------
          #include <gtest/gtest.h
          #include <iostream>
          int main(int argc, char **argv) {
          testing::InitGoogleTest(&argc, argv);
          return RUN_ALL_TESTS();
          }
          ----------------運行--------------------------
          gcc $(gtest-config --ldflags --libs) -o main_test code.cpp code_test.cpp main_test.cpp -I /home/sosotest/include -lgtest -L /home/sosotest/lib
          ----------------運行--------------------------
          ./main_test
          [==========] Running 4 tests from 4 test cases.
          [----------] Global test environment set-up.
          [----------] 1 test from TEST_A
          [ RUN      ] TEST_A.NAME
          [       OK ] TEST_A.NAME (0 ms)
          [----------] 1 test from TEST_A (0 ms total)
          [----------] 1 test from TEST_B
          [ RUN      ] TEST_B.NAME
          [       OK ] TEST_B.NAME (0 ms)
          [----------] 1 test from TEST_B (0 ms total)
          [----------] Global test environment tear-down
          [==========] 2 tests from 2 test cases ran. (0 ms total)
          [  PASSED  ] 2 tests.

           ===========windows運行gtest=================
            STEP1: 下載cygwin安裝,默認在c:\cygwin
            STEP2: 下載gtest1.6,解壓并拷貝到cygwin的目錄下,即c:\cygwin的某個子目錄下
            STEP3: hack src/gtest.cc:808行的gettimeofday(window沒有這個函數,參考http://blog.chinaunix.net/uid-140978-id-3029195.html)
            替換 :gettimeofday(&now, NULL);
            替換成:
            FILETIME ft;
            long long *time64 = (long long *) &ft;
            GetSystemTimeAsFileTime (&ft);
            *time64 -= 116444736000000000LL;
            *time64 /= 10;
            now.tv_sec = *time64 / 1000000;
            now.tv_usec = *time64 % 1000000;
            STEP4: 進入make子目錄,去掉Makefile中的最后一行的 -lpthread(window無pthread庫)
            STEP5: 執行make,生成并運行sample1_unittest.exe,截圖略

          posted on 2014-03-20 10:55 順其自然EVO 閱讀(448) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

          <2014年3月>
          2324252627281
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 崇明县| 格尔木市| 钟祥市| 汉沽区| 泰州市| 朔州市| 邹平县| 陈巴尔虎旗| 荥阳市| 望江县| 滦平县| 宝丰县| 赤水市| 宝山区| 息烽县| 县级市| 页游| 烟台市| 措美县| 泽州县| 龙泉市| 浑源县| 怀安县| 泰顺县| 溧阳市| 巴彦县| 读书| 大邑县| 蒙阴县| 通许县| 客服| 曲阜市| 普兰店市| 无为县| 浙江省| 兴安县| 天镇县| 宁武县| 宜阳县| 潜山县| 岳阳市|