用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中,单元测试的功能很强大,使得建立单元测试和编写单元测试代码,以及管理和运行单元测试都变得简单起来,通过私有访问器可以对私有方法 ...
随机推荐
- php 获取目录下文件列表
可以用 scandir() 函数 例如: http://www.w3school.com.cn/php/func_directory_scandir.asp
- FlexboxLayout 的一些基本介绍与基本用法
1什么是 Flexbox 简单来说 Flexbox 是属于web前端领域CSS的一种布局方案,是2009年W3C提出了一种新的布局方案,可以简便.完整.响应式地实现各种页面布局,并且 React Na ...
- Linux进程管理—信号、定时器
信号: 1. 信号的作用: 背景: 进程之间通信比较麻烦. 但进程之间又必须通信,比如父子进程之间. 作用: 通知其他进程响应.进程之间的一种通信机制. 信号: 接受信号的进程马上停止,调 ...
- http://poj.org/problem?id=2253
floyd的应用求每条路径两点之间最大距离的最小值 #include <iostream> #include <cstdio> #include <algorithm&g ...
- 经典SQL语句大全之数据开发
数据开发 1.按姓氏笔画排序:Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多 ...
- 在Eclipse上搭建Cocos2d-x的Android开发环境
很多其它相关内容请查看本人博客:http://www.bokeyi.com/ll/category/cocos2d-x/ 本文的搭建方法是最新最正确的方法,好多朋友反映搭建eclipse交叉编译环境非 ...
- The Class Loader Hierarchy--转载
Class loaders in the Application Server runtime follow a delegation hierarchy that is illustrated in ...
- myeclipse设置技巧
如何设置jsp的默认打开为MyEclipse JSP Editor? windows -> General -> Editor - > File Associations 选择 *. ...
- ios下微信标题修改
很多开发过微信的人估计都遇到过这样的问题,ios下微信页面标题更改不了,而安卓却可以直接用:document.title="你的标题". 下面是解决这个问题的hack: 1.jqu ...
- Nginx性能统计模块http_stub_status_module使用
1.进入nginx源码目录,重新配置编译参数 ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module 2.重新编译安 ...