qileilove

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

          gtest參數化測試代碼示例

           在玩轉 Google開源C++單元測試框架Google Test系列(gtest)之四 - 參數化中已經介紹過了如何使用gtest進行參數化測試。在twitter上應 @xlinker 的要求,我在這里提供一個參數化的完整例子。這個例子也是我當初了解gtest時寫的,同時這個例子也在《玩轉》系列中出現過。最后,我再附上整個demo工程,里面有一些其他的示例,剛開始上手的同學可以直接拿我的demo工程去試,有任何疑問都歡迎提出。以下是使用TEST_P宏進行參數化測試的示例:
          #include "stdafx.h"
          #include "foo.h"
          #include <gtest/gtest.h>
          class IsPrimeParamTest : public::testing::TestWithParam<int>
          {
          };
          // 不使用參數化測試,就需要像這樣寫五次
          TEST(IsPrimeTest, HandleTrueReturn)
          {
          EXPECT_TRUE(IsPrime(3));
          EXPECT_TRUE(IsPrime(5));
          EXPECT_TRUE(IsPrime(11));
          EXPECT_TRUE(IsPrime(23));
          EXPECT_TRUE(IsPrime(17));
          }
          // 使用參數化測試,只需要:
          TEST_P(IsPrimeParamTest, HandleTrueReturn)
          {
          int n =  GetParam();
          EXPECT_TRUE(IsPrime(n));
          }
          // 定義參數
          INSTANTIATE_TEST_CASE_P(TrueReturn, IsPrimeParamTest, testing::Values(3, 5, 11, 23, 17));
          // -----------------------
          // 更復雜一點的參數結構
          struct NumberPair
          {
          NumberPair(int _a, int _b)
          {
          a = _a;
          b = _b;
          }
          int a;
          int b;
          };
          class FooParamTest : public ::testing::TestWithParam<NumberPair>
          {
          };
          TEST_P(FooParamTest, HandleThreeReturn)
          {
          FooCalc foo;
          NumberPair pair = GetParam();
          EXPECT_EQ(3, foo.Calc(pair.a, pair.b));
          }
          INSTANTIATE_TEST_CASE_P(ThreeReturn, FooParamTest, testing::Values(NumberPair(12, 15), NumberPair(18, 21)));

          posted on 2013-11-07 11:13 順其自然EVO 閱讀(293) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 奉节县| 新巴尔虎右旗| 顺昌县| 汝阳县| 榆树市| 鄂温| 邹平县| 阳江市| 方正县| 济南市| 隆昌县| 沁水县| 桃江县| 成都市| 彭山县| 清镇市| 南川市| 应城市| 珠海市| 疏勒县| 芦山县| 临清市| 辽源市| 万荣县| 五家渠市| 江油市| 二连浩特市| 龙游县| 两当县| 商水县| 高清| 桂阳县| 九龙城区| 绥芬河市| 张家界市| 彭阳县| 富锦市| 嵊泗县| 竹溪县| 巫山县| 宜昌市|