51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2496|回复: 3
打印 上一主题 下一主题

[转贴] 用 gtest 实现数据驱动的单元测试

[复制链接]
  • TA的每日心情
    无聊
    2024-7-12 13:16
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2017-6-15 10:02:36 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
    后端技术在testhome算不上火热,贴代码选项里面竟然没有c这个项目,这里列举个gtest的例子
    1. /使用gtest进行数据驱动的单元测试

    2. #include <gtest/gtest.h>
    3. #include <iostream>
    4. #include <vector>
    5. #include <string>
    6. #include <fstream>
    7. #include "BuildAttrDesc.h"
    8. using namespace std;
    9. using namespace testing;
    10. //定义一个结构体,用于保存输入数据和期望结果的对比数据
    11. typedef struct
    12. {
    13.    string myString;
    14.    string productId;
    15.    string standardAttr;
    16.    string customAttr;
    17.    string resultsExpect;
    18.    string attrValueExpect;
    19. }datatype;
    20. //把vector返回给TEST_P,然后由TEST_P来处理相关的数据
    21. typedef ::std::vector<datatype> data;
    22. static data vec;
    23. //声明一个测试类,用来进行参数传递的类
    24. /*
    25. To write value-parameterized tests, first you should define a fixture class.
    26. It must be derived from both ::testing::Test and ::testing::WithParamInterface<T> (the latter is a pure interface),
    27. where T is the type of your parameter values. For convenience, you can just derive the fixture class from
    28. ::testing::TestWithParam<T>, which itself is derived from both ::testing::Test and ::testing::WithParamInterface<T>.
    29. T can be any copyable type. If it's a raw pointer, you are responsible for managing the lifespan of the pointed values.
    30. */
    31. class testBuildAttrDesc : public::testing::TestWithParam<datatype>{};

    32. /*
    33. The following class reads the envonrimental data from file "./dump_data/commodity_attribute_name_en","./dump_data/commodity_value_name_en",
    34. every row consisted of the input and expected output from file "./dump_data/product_attribute/standard_custom_expect_attr.txt" and then push them into the vector
    35. */
    36. class Singleton
    37. {
    38. public:
    39.     CBuildAttrDesc* m_pBuildAttrDesc;
    40.     static Singleton* getInstance()
    41.     {
    42.         if(_instance==NULL) _instance=new Singleton();
    43.             return _instance;
    44.     }
    45.     ~Singleton()
    46.     {
    47.         delete m_pBuildAttrDesc;
    48.         m_pBuildAttrDesc = NULL;
    49.     }
    50. protected:
    51.     Singleton()
    52.     {
    53.         datatype d;
    54.         const string recordSeparator = "\001\003";
    55.         const string fieldSeparator = "\001\002";
    56.         string myString = "";
    57.         string productId = "";
    58.         string standardAttr = "";
    59.         string customAttr = "";
    60.         string resultsExpect = "";
    61.         string attrValueExpect = "";
    62.         vector<string> vStrArray;
    63.         m_pBuildAttrDesc = new CBuildAttrDesc;
    64.         m_pBuildAttrDesc->loadAttrNameValueMap("./dump_data/commodity_attribute_name_en","./dump_data/commodity_value_name_en", recordSeparator, fieldSeparator);
    65.         if (!m_pBuildAttrDesc->initOk())
    66.         {
    67.             cout<<"CBuildAttrDesc init failed!"<<endl;
    68.             exit(1);
    69.         }else
    70.             cout<<endl<<"sucess: Open files commodity_attribute_name_en.dump and commodity_value_name_en.dump"<<endl<<endl;
    71.         ifstream inData("./dump_data/product_attribute/standard_custom_expect_attr.txt");//open the input files
    72.         while(getline(inData, myString))
    73.         {
    74.             m_pBuildAttrDesc->splitStr(myString, "", vStrArray);
    75.             if(5 != vStrArray.size())//total have five segments
    76.             {
    77.             cout<<"Format Error(the total record): "<<myString<<endl;
    78.             continue;
    79.             }
    80.             d.productId = vStrArray[0];
    81.             d.standardAttr = vStrArray[1];
    82.             d.customAttr = vStrArray[2];
    83.             d.resultsExpect = vStrArray[3];
    84.             d.attrValueExpect = vStrArray[4];
    85.             if(m_pBuildAttrDesc->isDigits(d.productId)==false)
    86.             {
    87.              cout<<"Format Error(the product id): "<<d.productId<<endl;
    88.              continue;
    89.             }
    90.             cout<<endl<<"product id:     "<<d.productId<<endl;
    91.             cout<<endl<<"standardAttr:   "<<d.standardAttr<<endl;
    92.             cout<<endl<<"customAttr:     "<<d.customAttr<<endl;
    93.             cout<<endl<<"resultsExpect:  "<<d.resultsExpect<<endl;
    94.             cout<<endl<<"attrValueExpect:"<<d.attrValueExpect<<endl;
    95.             vec.push_back(d);
    96.          }
    97.     }
    98. private:
    99.         static Singleton *_instance;
    100. };
    101. Singleton* Singleton::_instance = NULL ;
    102. Singleton *single=Singleton::getInstance();


    103. /*
    104. Then, use the TEST_P macro to define as many test patterns using this fixture as you want. The _P suffix is for
    105. "parameterized" or "pattern", whichever you prefer to think.
    106. */
    107. TEST_P(testBuildAttrDesc,HandleTrueReturn)
    108. {
    109.     datatype n = GetParam();
    110.     string results = "";
    111.     string attrValue = "";
    112.     // expected test
    113.     single->m_pBuildAttrDesc->getAttrDesc(n.standardAttr,n.customAttr,results,attrValue);
    114.     cout<<n.standardAttr<<endl<<n.customAttr<<endl<<results<<endl<<attrValue<<endl;
    115.     EXPECT_STREQ(n.resultsExpect.c_str(),results.c_str());
    116.     EXPECT_STREQ(n.attrValueExpect.c_str(),attrValue.c_str());
    117. }
    118. /*
    119. Finally, you can use INSTANTIATE_TEST_CASE_P to instantiate the test case with any set of parameters you want. Google Test
    120. defines a number of functions for generating test parameters. They return what we call (surprise!) parameter generators.
    121. Here is a summary of them, which are all in the testing namespace:
    122. */
    123. INSTANTIATE_TEST_CASE_P(TrueReturn,testBuildAttrDesc,::testing::ValuesIn(vec));

    124. int main(int argc,char *argv[])
    125. {
    126.     testing::InitGoogleTest(&argc,argv);
    127.     RUN_ALL_TESTS();
    128.     delete single;
    129.     return 0;
    130. }
    复制代码


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    该用户从未签到

    4#
    发表于 2017-6-16 17:18:49 | 只看该作者
    先浏览,留言咯。默默围观。
    学习共同成长,版主棒棒的
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2024-7-12 13:16
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    3#
     楼主| 发表于 2017-6-15 10:27:34 | 只看该作者
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-25 16:46 , Processed in 0.066069 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表