用gtest实现数据驱动的单元测试
//使用gtest进行数据驱动的单元测试 #include <gtest/gtest.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "BuildAttrDesc.h"
using namespace std;
using namespace testing;
//定义一个结构体,用于保存输入数据和期望结果的对比数据
typedef struct
{
string myString;
string productId;
string standardAttr;
string customAttr;
string resultsExpect;
string attrValueExpect;
}datatype;
//把vector返回给TEST_P,然后由TEST_P来处理相关的数据
typedef ::std::vector<datatype> data;
static data vec;
//声明一个测试类,用来进行参数传递的类
/*
To write value-parameterized tests, first you should define a fixture class.
It must be derived from both ::testing::Test and ::testing::WithParamInterface<T> (the latter is a pure interface),
where T is the type of your parameter values. For convenience, you can just derive the fixture class from
::testing::TestWithParam<T>, which itself is derived from both ::testing::Test and ::testing::WithParamInterface<T>.
T can be any copyable type. If it's a raw pointer, you are responsible for managing the lifespan of the pointed values.
*/
class testBuildAttrDesc : public::testing::TestWithParam<datatype>{}; /*
The following class reads the envonrimental data from file "./dump_data/commodity_attribute_name_en","./dump_data/commodity_value_name_en",
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
*/
class Singleton
{
public:
CBuildAttrDesc* m_pBuildAttrDesc;
static Singleton* getInstance()
{
if(_instance==NULL) _instance=new Singleton();
return _instance;
}
~Singleton()
{
delete m_pBuildAttrDesc;
m_pBuildAttrDesc = NULL;
}
protected:
Singleton()
{
datatype d;
const string recordSeparator = "\001\003";
const string fieldSeparator = "\001\002";
string myString = "";
string productId = "";
string standardAttr = "";
string customAttr = "";
string resultsExpect = "";
string attrValueExpect = "";
vector<string> vStrArray;
m_pBuildAttrDesc = new CBuildAttrDesc;
m_pBuildAttrDesc->loadAttrNameValueMap("./dump_data/commodity_attribute_name_en","./dump_data/commodity_value_name_en", recordSeparator, fieldSeparator);
if (!m_pBuildAttrDesc->initOk())
{
cout<<"CBuildAttrDesc init failed!"<<endl;
exit(1);
}else
cout<<endl<<"sucess: Open files commodity_attribute_name_en.dump and commodity_value_name_en.dump"<<endl<<endl;
ifstream inData("./dump_data/product_attribute/standard_custom_expect_attr.txt");//open the input files
while(getline(inData, myString))
{
m_pBuildAttrDesc->splitStr(myString, "", vStrArray);
if(5 != vStrArray.size())//total have five segments
{
cout<<"Format Error(the total record): "<<myString<<endl;
continue;
}
d.productId = vStrArray[0];
d.standardAttr = vStrArray[1];
d.customAttr = vStrArray[2];
d.resultsExpect = vStrArray[3];
d.attrValueExpect = vStrArray[4];
if(m_pBuildAttrDesc->isDigits(d.productId)==false)
{
cout<<"Format Error(the product id): "<<d.productId<<endl;
continue;
}
cout<<endl<<"product id: "<<d.productId<<endl;
cout<<endl<<"standardAttr: "<<d.standardAttr<<endl;
cout<<endl<<"customAttr: "<<d.customAttr<<endl;
cout<<endl<<"resultsExpect: "<<d.resultsExpect<<endl;
cout<<endl<<"attrValueExpect:"<<d.attrValueExpect<<endl;
vec.push_back(d);
}
}
private:
static Singleton *_instance;
};
Singleton* Singleton::_instance = NULL ;
Singleton *single=Singleton::getInstance(); /*
Then, use the TEST_P macro to define as many test patterns using this fixture as you want. The _P suffix is for
"parameterized" or "pattern", whichever you prefer to think.
*/
TEST_P(testBuildAttrDesc,HandleTrueReturn)
{
datatype n = GetParam();
string results = "";
string attrValue = "";
// expected test
single->m_pBuildAttrDesc->getAttrDesc(n.standardAttr,n.customAttr,results,attrValue);
cout<<n.standardAttr<<endl<<n.customAttr<<endl<<results<<endl<<attrValue<<endl;
EXPECT_STREQ(n.resultsExpect.c_str(),results.c_str());
EXPECT_STREQ(n.attrValueExpect.c_str(),attrValue.c_str());
}
/*
Finally, you can use INSTANTIATE_TEST_CASE_P to instantiate the test case with any set of parameters you want. Google Test
defines a number of functions for generating test parameters. They return what we call (surprise!) parameter generators.
Here is a summary of them, which are all in the testing namespace:
*/
INSTANTIATE_TEST_CASE_P(TrueReturn,testBuildAttrDesc,::testing::ValuesIn(vec)); int main(int argc,char *argv[])
{
testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
delete single;
return 0;
}
gtest官方关于数据驱动单元测试的文档
http://code.google.com/p/googletest/wiki/AdvancedGuide
用gtest实现数据驱动的单元测试的更多相关文章
- vs2015数据驱动的单元测试
今天在做测试的时候boss让我这个菜鸟做vs2015下c#的单元测试,并且给了我参考http://www.cnblogs.com/kingmoon/archive/2011/05/13/2045278 ...
- 使用gtest(googletest)进行c++单元测试
这是系列文章的第三篇,前两篇https://www.cnblogs.com/gaopang/p/11243367.html和https://www.cnblogs.com/gaopang/p/1158 ...
- ABP中单元测试的技巧:Mock和数据驱动
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:虽然ABP为大家提供了测试的脚手架了,不过有些小技巧还是需要自己探索的. ASP.NE ...
- linux下使用gtest框架进行c/c++单元测试
linux下使用gtest框架进行c/c++单元测试 前言 关于此次开发工具的选择,因为我最近尝试在linux下使用vim进行c/c++编程,且之前已经对vim进行了相关的配置,所以这里应作业要求直接 ...
- gtest官方文档浅析
gtest的所有官方文档:http://code.google.com/p/googletest/w/list 选择单元测试框架的那些事 gtest不是唯一开源的单元测试框架,我也不觉得它是最好的单元 ...
- 玩转Google开源C++单元测试框架Google Test系列(转载)
越来越多公司采用敏捷开发,单元和回归测试越来越重要,GTest作为最佳C++单元测试工具越来越多的被使用.转自 http://www.cnblogs.com/coderzh/archive/2009/ ...
- 如何用googletest写单元测试
http://www.uml.org.cn/c++/201203293.asp googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用在windows.linux.Mac等OS平台 ...
- Python 数据驱动 unittest + ddt
一数据驱动测试的含义: 在百度百科上的解释是: 数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子.利用黑盒测试法进行动态测试时,需要测试软件产 ...
- [转]Visual Studio 2010单元测试(1)--运行和定义普通单元测试
Visual Studio 2010 运行和定义单元测试 在VS2010中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...
随机推荐
- Java Development Kit(JDK) 8 新特性(简述)
一.接口的默认方法 Java 8允许我们给接口添加一个非抽象的方法实现,只需要使用 default关键字即可,这个特征又叫做扩展方法. 示例如下: interface Formula { calcul ...
- Java编程性能优化一
转自:http://my.oschina.net/xianggao/blog/77224 在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著 ...
- linux ssh scp无密码登录
一. 应用场景 假如你Linux Client是客户端, Server为服务器,用户名为user.现在要配置从Client到Server的无密码SSH登录或者无密码的scp拷贝. 例如客户端Clien ...
- 王灏:光音网络致力打造Wi-Fi大生态圈
光音网络,做的是本地网络综合服务.在中国,想把互联网做到覆盖延伸范围之外的最后100米,光音网络是当中一家,也是最坚持的一家.为千万家本地生活商户提供帮助,为数亿本地用户提供最佳的本地网络体验,这是光 ...
- android79 Fragment生命周期
切换成01时依次调用onCreate,onStart,onResume方法,切换到03的时候01依次onPause,onStop,onDestroy,03依次onCreate,onStart,onRe ...
- Cocos2d-x中常用粒子编辑器ParticleDesigner测试例子
打开 ParticleDesigner 随意选择一种效果 选择save. 类型选择 cocosd(plist)类型保存至桌面 命名为myplist导出至桌面 选择右侧Emitter Config可设置 ...
- oracle数据库使用之数据查询入门
1.在查询过程中使用算术表达式对数据进行运算 student表结构如下: 最后一项salary表示每个人的月薪,我现在想查询每个人的年薪: 2.使用nvl函数处理null值,向表中插入一条数据,该数据 ...
- ASP.NET基础之HttpContext学习
一:HttpContext理论知识: 1:HttpContext类它对Request.Respose.Server等等都进行了封装,并保证在整个请求周期内都可以随时随地的调用:为继承 IHttpMod ...
- ADO.Net技术
Connection对象 1.连接数据库 通过SqlConnection对象的State属性判断数据库的连接状态: public override ConnectionState State{ get ...
- SqlSugar简单工模式数据访问简单Demo
源代码地址:http://git.oschina.net/tiama3798/BootstrapBack_Demo 1.Model层 2.抽象层实例: 基础接口 /// <summary> ...