qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          gtest參數(shù)化測試代碼示例

           在玩轉(zhuǎn) Google開源C++單元測試框架Google Test系列(gtest)之四 - 參數(shù)化中已經(jīng)介紹過了如何使用gtest進(jìn)行參數(shù)化測試。在twitter上應(yīng) @xlinker 的要求,我在這里提供一個參數(shù)化的完整例子。這個例子也是我當(dāng)初了解gtest時寫的,同時這個例子也在《玩轉(zhuǎn)》系列中出現(xiàn)過。最后,我再附上整個demo工程,里面有一些其他的示例,剛開始上手的同學(xué)可以直接拿我的demo工程去試,有任何疑問都?xì)g迎提出。以下是使用TEST_P宏進(jìn)行參數(shù)化測試的示例:
          #include "stdafx.h"
          #include "foo.h"
          #include <gtest/gtest.h>
          class IsPrimeParamTest : public::testing::TestWithParam<int>
          {
          };
          // 不使用參數(shù)化測試,就需要像這樣寫五次
          TEST(IsPrimeTest, HandleTrueReturn)
          {
          EXPECT_TRUE(IsPrime(3));
          EXPECT_TRUE(IsPrime(5));
          EXPECT_TRUE(IsPrime(11));
          EXPECT_TRUE(IsPrime(23));
          EXPECT_TRUE(IsPrime(17));
          }
          // 使用參數(shù)化測試,只需要:
          TEST_P(IsPrimeParamTest, HandleTrueReturn)
          {
          int n =  GetParam();
          EXPECT_TRUE(IsPrime(n));
          }
          // 定義參數(shù)
          INSTANTIATE_TEST_CASE_P(TrueReturn, IsPrimeParamTest, testing::Values(3, 5, 11, 23, 17));
          // -----------------------
          // 更復(fù)雜一點的參數(shù)結(jié)構(gòu)
          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 閱讀(292) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          <2013年11月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西乡县| 葵青区| 拜城县| 湘潭县| 自贡市| 宜昌市| 保山市| 邢台县| 鄢陵县| 高雄县| 虹口区| 日照市| 临邑县| 咸丰县| 南康市| 明溪县| 介休市| 密云县| 南江县| 西安市| 合江县| 灵石县| 潞西市| 滨海县| 甘谷县| 思南县| 江阴市| 彭州市| 缙云县| 孝昌县| 大名县| 黄梅县| 沧源| 木兰县| 涞水县| 盖州市| 恩施市| 连州市| 镇康县| 都匀市| 大荔县|