用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中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...
随机推荐
- Linux无处不在
Linux is Everywhere从政府.教育.商业和非盈利组织.科研机构几个方面展示了现在都有哪些地方用了Linux.
- 6步骤实现CentOS系统环境精简优化
6步骤实现CentOS系统环境精简优化 发布时间:2014-11-03 14:59:27 编辑:AHLinux.com 第一步.删除不必要的自带软件包yum remove Deployment_G ...
- Oracle DB 备份和恢复的概念
• 确定Oracle DB 中可能发生的故障类型 • 说明优化实例恢复的方法 • 说明检查点.重做日志文件和归档日志文件的重要性 • 配置快速恢复区 • 配置ARCHIVELOG模式 部分工作内容 ...
- There is an error while getting planid. No Free partitions available
问题概述 Oracle Advanced Supply Chain Planning最初的设置职责的时候有点问题,不知是不是要打什么补丁或其它配置什么东东,, 这个提示,,但我查到的分区是还有可用分区 ...
- IOPS QPS TPS
杨奇龙: http://blog.itpub.net/22664653/viewspace-767265/ http://blog.itpub.net/22664653/viewspace-76726 ...
- WCF - 实例与会话
实例上下文 实例上下文是对服务实例的封装 是WCF管理服务实例生命周期的依托 一个WCF服务通过ServiceHost进行寄宿 开启服务后当接收到请求 则会判断当前是否存在实例上下文 如果存在 则通 ...
- JS家的排序算法
由于浏览器的原生支持(无需安装任何插件),用JS来学习数据结构和算法也许比c更加便捷些.因为只需一个浏览器就能啪啪啪的调试了.比如下图我学习归并排序算法时,只看代码感觉怎么都理解不了,但是结合chro ...
- oracle用户管理实例
oracle中的用户角色分为预定义角色和自定义角色. 角色是把常用的权限集中起来形成角色. 授权/分配角色命令 grant 权限/角色 to 用户 收回权限命令: revoke 综合案例: 创建一个用 ...
- Oracle11g安装完成后给用户解锁
安装时候你可能忘记给普通用户scott解锁,导致安装成功后普通用户无法登录,那该怎么办呢? 先用system用户登录,登录成功之后就可以给其他用户解锁了. 如图: 同理,如果要锁定某一个用户,只需要把 ...
- pnd3
这两天重写了,消除后本身的下落计算还有问题,新产生的块下落和消除已经OK了.消除算法真的很要命.最后还是回归最开始的想法,用递归的方式不断的SPREAD来得到消除的数据.快到月底了,得勤写写了,要不找 ...