用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中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...
随机推荐
- C++的静态分发(CRTP)和动态分发(虚函数多态)的比较
虚函数是C++实现多态的工具,在运行时根据虚表决定调用合适的函数.这被称作动态分发.虚函数很好的实现了多态的要求,但是在运行时引入了一些开销,包括: 对每一个虚函数的调用都需要额外的指针寻址 虚函数通 ...
- Conditionals with Omitted Operands (x ? : y)
Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Th ...
- C++中this指针的使用方法.
this指针仅仅能在一个类的成员函数中调用,它表示当前对象的地址.以下是一个样例: void Date::setMonth( int mn ) { month = mn; // 这三句是等价的 thi ...
- Intent 传值和 Bundle传值的区别
http://blog.csdn.net/yanzi1225627/article/details/7802819 举个例子 我现在要从A界面 跳转到B界面或者C界面 这样的话 我就需要写2 ...
- ZooKeeper监控
http://jm-blog.aliapp.com/?p=1450 在公司内部,有不少应用已经强依赖zookeeper,zookeeper的工作状态直接影响它们的正常工作.目前开源世界中暂没有一个比较 ...
- lua wireshark 数据报解析
http://www.360doc.com/content/13/1226/15/15257968_340284574.shtml http://www.360doc.com/userhome.asp ...
- hdu2025java字符题
查找最大元素 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 怎样通过ajax提交数据
ajax的出现彻底改变了javascript命运,通过ajax可以直接向服务器提交数据,有两种方式: get方式,数据直接拼接在地址中 post方式,数据由data字段携带 post方式,data中是 ...
- css3 盒模型
0,前言 在css2.1 之前,我们都熟知的两种盒模型,一种是w3c标准盒模型,另外一种是怪异模式下的盒模型.在css3之前我们一直使用的是标准盒模型,但是标准盒模型的宽度总是需要小心的去使用,稍有不 ...
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...