python--生成测试数据
1.方法
import datetime params_dict_data = {
"system_id":"systemId001",
"order_date":"20170918",
"order_id": "201799999999",
"user_name": "test",
"service_name": "me",
"begin_time": "20170918000000",
"end_time": "20170930000000",
}
"""
1.最普通:必填限制、长度限制(定长or最大长度)
2.较普通:必填限制、长度限制、字符类型限制
3.枚举型 普遍参数:
1.日期型
2.时间型
3.手机号
4.身份证
5.邮箱 """
# 枚举型
# params_dict正确的一条字典数据 # 所有enum_list枚举值 #
def enum_param(params_dict, param_name, enum_list):
case_data_list = []
if type(params_dict) == dict and type(enum_list) == list:
#正常值
for enum_value in enum_list:
params_dict[param_name] = enum_value
case_data_list.append(params_dict.copy())
#为了恢复字典值
recover_value = params_dict[param_name]
# 含特殊字符
param_name_value = params_dict[param_name]
param_name_value1 = param_name_value[0:len(param_name_value) - 1] + '*'
params_dict[param_name] = param_name_value1
case_data_list.append(params_dict.copy())
# 不在枚举中的值
param_name_value = params_dict[param_name]
param_name_value2 = param_name_value[0:len(param_name_value)-1] + 'A'
params_dict[param_name] = param_name_value2
case_data_list.append(params_dict.copy())
# 不在枚举中的值
param_name_value = params_dict[param_name]
param_name_value2 = param_name_value[0:len(param_name_value) - 1] + '99'
params_dict[param_name] = param_name_value2
case_data_list.append(params_dict.copy())
# 不在枚举中的值
param_name_value = params_dict[param_name]
param_name_value2 = param_name_value[0:len(param_name_value) - 2]
params_dict[param_name] = param_name_value2
case_data_list.append(params_dict.copy())
#空值
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = recover_value
return case_data_list
else:
print('参数有误勒') # 日期型
def data_param(params_dict, param_name):
case_data_list = []
current_date = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d')
current_date_1 = datetime.datetime.strftime(datetime.datetime.now()-datetime.timedelta(1), '%Y%m%d')
current_date_2 = datetime.datetime.strftime(datetime.datetime.now() - datetime.timedelta(2), '%Y%m%d')
if type(params_dict) == dict and param_name:
# 昨天
params_dict[param_name] = current_date_1
case_data_list.append(params_dict.copy())
# 前天
params_dict[param_name] = current_date_2
case_data_list.append(params_dict.copy())
# 当前日期
params_dict[param_name] = current_date
case_data_list.append(params_dict.copy())
# 长度+1
params_dict[param_name] = current_date + '0'
case_data_list.append(params_dict.copy())
# 长度-1
params_dict[param_name] = current_date[0:len(current_date)-1]
case_data_list.append(params_dict.copy())
# 含特殊字符
params_dict[param_name] = current_date[0:len(current_date)-1] + '*'
case_data_list.append(params_dict.copy())
# 含字母
params_dict[param_name] = current_date[0:len(current_date)-1] + 'A'
case_data_list.append(params_dict.copy())
# 为空
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = current_date
return case_data_list
else:
print('参数错误勒!') # 时间型
def time_param(params_dict, param_name):
case_data_list = []
current_date = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S')
current_date_1 = datetime.datetime.strftime(datetime.datetime.now() - datetime.timedelta(1), '%Y%m%d%H%M%S')
current_date_2 = datetime.datetime.strftime(datetime.datetime.now() - datetime.timedelta(2), '%Y%m%d%H%M%S')
if type(params_dict) == dict and param_name:
# 昨天
params_dict[param_name] = current_date_1
case_data_list.append(params_dict.copy())
# 前天
params_dict[param_name] = current_date_2
case_data_list.append(params_dict.copy())
# 当前日期
params_dict[param_name] = current_date
case_data_list.append(params_dict.copy())
# 长度+1
params_dict[param_name] = current_date + '0'
case_data_list.append(params_dict.copy())
# 长度-1
params_dict[param_name] = current_date[0:len(current_date) - 1]
case_data_list.append(params_dict.copy())
# 含特殊字符
params_dict[param_name] = current_date[0:len(current_date) - 1] + '*'
case_data_list.append(params_dict.copy())
# 含字母
params_dict[param_name] = current_date[0:len(current_date) - 1] + 'A'
case_data_list.append(params_dict.copy())
# 为空
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = current_date
return case_data_list
else:
print('参数错误勒!') #手机号类型
def mobile_param(params_dict, param_name):
case_data_list = []
mobile_param = params_dict[param_name]
if type(params_dict) == dict and param_name:
# 正常
case_data_list.append(params_dict.copy())
# 含非数字字符
params_dict[param_name] = mobile_param[0:10] + 'a'
case_data_list.append(params_dict.copy())
# 含特殊字符
params_dict[param_name] = mobile_param[0:10] + '*'
case_data_list.append(params_dict.copy())
# 长度12
params_dict[param_name] = mobile_param + '0'
case_data_list.append(params_dict.copy())
# 长度10
params_dict[param_name] = mobile_param[0:10]
case_data_list.append(params_dict.copy())
# 空
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = mobile_param
return case_data_list
else:
print('参数有问题勒!') # 身份证型
def idcard_param(params_dict, param_name):
case_data_list = []
idcard_param = params_dict[param_name]
if type(params_dict) == dict and param_name:
# 正常
case_data_list.append(params_dict.copy())
# 含特殊字符
params_dict[param_name] = '*' + idcard_param[1:len(idcard_param)]
case_data_list.append(params_dict.copy())
# 长度17
params_dict[param_name] = idcard_param[0:len(idcard_param)-1]
case_data_list.append(params_dict.copy())
# 长度19
params_dict[param_name] = idcard_param + '0'
case_data_list.append(params_dict.copy())
# 空
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = idcard_param
return case_data_list # 邮箱型
def email_param(params_dict, param_name):
case_data_list = []
email_param = params_dict[param_name]
if type(params_dict) == dict and param_name:
email_param_1 = '110@163.com'
email_param_2 = 'qi.fan@chianpnr.com'
email_param_3 = '1377@qq.com'
email_param_4 = '135qf@qq.com'
# 正常
params_dict[param_name] = email_param_1
case_data_list.append(params_dict.copy())
params_dict[param_name] = email_param_2
case_data_list.append(params_dict.copy())
params_dict[param_name] = email_param_3
case_data_list.append(params_dict.copy())
params_dict[param_name] = email_param_4
case_data_list.append(params_dict.copy())
# 空
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = email_param
return case_data_list
else:
print('参数错误!') # 长度
def length_check_param(params_dict, param_name, max_length, min_length=None):
case_data_list = []
need_check_param = params_dict[param_name]
if type(params_dict) == dict and param_name and max_length:
# 最大长度
if len(need_check_param) < max_length:
need_check_param_1 = need_check_param + '0' * (max_length - len(need_check_param))
params_dict[param_name] = need_check_param_1
case_data_list.append(params_dict.copy())
# 最大长度+1
params_dict[param_name] = need_check_param_1 + '0'
case_data_list.append(params_dict.copy())
elif len(need_check_param) == max_length:
case_data_list.append(params_dict.copy())
params_dict[param_name] = need_check_param + '0'
case_data_list.append(params_dict.copy())
else:
print('参数错误!')
# 最小长度
if min_length != None and min_length != '':
if len(need_check_param) >= min_length:
# 最小长度
need_check_param_2 = need_check_param[0:min_length]
params_dict[param_name] = need_check_param_2
case_data_list.append(params_dict.copy())
# 最小长度-1
params_dict[param_name] = need_check_param_2[0:min_length-1]
case_data_list.append(params_dict.copy())
else:
# 最小长度
need_check_param_2 = need_check_param + '0' * (min_length-len(need_check_param))
params_dict[param_name] = need_check_param_2
case_data_list.append(params_dict.copy())
# 最小长度-1
params_dict[param_name] = need_check_param_2[0:min_length - 1]
case_data_list.append(params_dict.copy())
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
# 中间长度
params_dict[param_name] = '9' * (int(max_length/2))
case_data_list.append(params_dict.copy())
params_dict[param_name] = need_check_param
return case_data_list # 最大值,最小值
def value_check_param(params_dict, param_name, max_value, min_value=0, param_type='int'):
case_data_list = []
value_p = params_dict[param_name]
if type(params_dict) == dict and param_name and max_value:
if param_type == 'int':
#最大值
params_dict[param_name] = str(max_value)
case_data_list.append(params_dict.copy())
#最大值-1
params_dict[param_name] = str(max_value-1)
case_data_list.append(params_dict.copy())
if min_value >= 0:
# 最小值
params_dict[param_name] = str(min_value)
case_data_list.append(params_dict.copy())
# 最小值-1
params_dict[param_name] = str(min_value - 1)
case_data_list.append(params_dict.copy())
else:
print('min_value参数错误')
params_dict[param_name] = str(max_value - 1)
case_data_list.append(params_dict.copy())
else:
# 最大值
params_dict[param_name] = str(max_value)
case_data_list.append(params_dict.copy())
# 最大值-1
params_dict[param_name] = str(max_value - 0.01)
case_data_list.append(params_dict.copy())
if min_value >= 0:
# 最小值
params_dict[param_name] = str(min_value)
case_data_list.append(params_dict.copy())
# 最小值-1
params_dict[param_name] = str(min_value - 0.01)
case_data_list.append(params_dict.copy())
else:
print('min_value参数错误')
params_dict[param_name] = str(max_value - 1)
case_data_list.append(params_dict.copy())
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = '-0.01'
case_data_list.append(params_dict.copy())
params_dict[param_name] = '-1'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p
return case_data_list
else:
print('参数错误啊弟弟妹妹!') # 字符类型限制 a-z A-Z a-Z 0-9 0-z 0-Z 0-z-Z
def char_type_param(params_dict, param_name, char_type):
case_data_list = []
value_p = params_dict[param_name]
if type(params_dict) == dict and param_name:
if char_type == '0-9':
params_dict[param_name] = value_p[0:len(value_p)-2] + 'aA'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == 'a-z':
params_dict[param_name] = value_p[0:len(value_p) - 1] + '1'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + 'A'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == 'A-Z':
params_dict[param_name] = value_p[0:len(value_p) - 1] + '1'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + 'a'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == 'a-Z':
params_dict[param_name] = value_p[0:len(value_p) - 1] + '1'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == '0-z':
params_dict[param_name] = value_p[0:len(value_p) - 1] + 'Z'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == '0-Z':
params_dict[param_name] = value_p[0:len(value_p) - 1] + 'z'
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
elif char_type == '0-z-Z':
params_dict[param_name] = value_p
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p[0:len(value_p) - 1] + '*'
case_data_list.append(params_dict.copy())
else:
print('char_type参数错误啊弟弟妹妹!')
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = value_p
return case_data_list
else:
print('参数错误啊弟弟妹妹!') def null_check_param(params_dict, param_name):
case_data_list = []
valuep = params_dict[param_name]
params_dict[param_name] = ''
case_data_list.append(params_dict.copy())
params_dict[param_name] = valuep
return case_data_list if __name__ == '__main__':
params_dict1 = {
"system_id": "systemId001",
"order_date": "20170918",
"order_id": "201799999999",
"b_user_name": "jf_test",
"service_name": "realme",
"begin_time": "20170918000000",
"end_time": "20170930000000",
"idcard":"362531199001010101"
}
enum_list = ['00', '01', '02', '03']
param_name = 'idcard'
result = char_type_param(params_dict1, param_name, char_type='a-z')
for result_one in result:
print(result_one)
print(int(1)) 2.调用
from temp import createParamsTestCase # param_description格式 是否特征参数|参数名|最大长度|最小长度|最大值|最小值|字符类型|枚举值 --> param_feature|name|maxlength=20|minlength=1|maxvalue=20|minvalue=2|chartype=a-z|00,01
# 是否特征参数:mobileP idcardP dateP timeP emailP enumP
class CreateTestCaseData: def __init__(self, params_dict, param_description_list):
self.__params_dict_data = params_dict
self.__param_description_list_operate = []
self.__p_d_d = {}
for param_description in param_description_list:
self.__param_description_list = param_description.split('|')
self.__param_feature = self.__param_description_list[0]
self.__param_name = self.__param_description_list[1]
self.__param_maxlength = self.__param_description_list[2]
self.__param_minlength = self.__param_description_list[3]
self.__param_maxvalue = self.__param_description_list[4]
self.__param_minvalue = self.__param_description_list[5]
if self.__param_maxlength != '':
self.__param_maxlength = int(self.__param_description_list[2])
# else:
# self.__param_maxlength = self.__param_description_list[2]
if self.__param_minlength != '':
self.__param_minlength = int(self.__param_description_list[3])
# else:
# self.__param_minlength = self.__param_description_list[3]
if self.__param_maxvalue != '':
self.__param_maxvalue = float(self.__param_description_list[4])
# else:
# self.__param_maxvalue = self.__param_description_list[4]
if self.__param_minvalue != '':
self.__param_minvalue = float(self.__param_description_list[5])
# else:
# self.__param_minvalue = self.__param_description_list[5]
self.__param_chartype = self.__param_description_list[6]
self.__param_enum_str = self.__param_description_list[7]
self.__param_enum = self.__param_enum_str.split(',')
self.__p_d_d['param_feature'] = self.__param_feature
self.__p_d_d['param_name'] = self.__param_name
self.__p_d_d['param_maxlength'] = self.__param_maxlength
self.__p_d_d['param_minlength'] = self.__param_minlength
self.__p_d_d['param_maxvalue'] = self.__param_maxvalue
self.__p_d_d['param_minvalue'] = self.__param_minvalue
self.__p_d_d['param_chartype'] = self.__param_chartype
self.__p_d_d['param_enum'] = self.__param_enum
self.__p_d_d['param_enum_str'] = self.__param_enum_str
self.__param_description_list_operate.append(self.__p_d_d.copy()) def produce_test_data(self):
self.__data_list = []
for _param_description_list_operate in self.__param_description_list_operate:
self._param_feature = _param_description_list_operate['param_feature']
self._param_name = _param_description_list_operate['param_name']
self._param_maxlength = _param_description_list_operate['param_maxlength']
self._param_minlength = _param_description_list_operate['param_minlength']
self._param_maxvalue = _param_description_list_operate['param_maxvalue']
self._param_minvalue = _param_description_list_operate['param_minvalue']
self._param_chartype = _param_description_list_operate['param_chartype']
self._param_enum_str = _param_description_list_operate['param_enum_str']
self._param_enum = _param_description_list_operate['param_enum']
if self._param_feature != '':
if self._param_feature == 'mobileP':
result = createParamsTestCase.mobile_param(self.__params_dict_data, self._param_name)
elif self._param_feature == 'idcardP':
result = createParamsTestCase.idcard_param(self.__params_dict_data, self._param_name)
elif self._param_feature == 'dateP':
result = createParamsTestCase.data_param(self.__params_dict_data, self._param_name)
elif self._param_feature == 'timeP':
result = createParamsTestCase.time_param(self.__params_dict_data, self._param_name)
elif self._param_feature == 'emailP':
result = createParamsTestCase.email_param(self.__params_dict_data, self._param_name)
elif self._param_feature == 'enumP':
result = createParamsTestCase.enum_param(self.__params_dict_data, self._param_name, self._param_enum)
elif self._param_feature == 'nullP':
result = createParamsTestCase.null_check_param(self.__params_dict_data, self._param_name)
else:
print('参数错误了弟弟妹妹!')
else:
if self._param_maxlength != '':
result = createParamsTestCase.length_check_param(self.__params_dict_data, self._param_name,
self._param_maxlength, self._param_minlength)
if self._param_minvalue != '':
result = createParamsTestCase.value_check_param(self.__params_dict_data, self._param_name,
self._param_maxvalue, self._param_minvalue,
param_type='f')
if self._param_chartype != '':
result = createParamsTestCase.char_type_param(self.__params_dict_data, self._param_name,
self._param_chartype)
self.__data_list.extend(result.copy())
return self.__data_list
if __name__ == '__main__':
params_dict1 = {
"order_date": "20171205",
"order_id": "1",
"mer_id": "111aaa",
"group_id": "a1b2c3d4e5",
"image": "C:\\Users\\Pictures\\3.jpg",
"file_fix": "jpg",
"file":"",
"number":"2",
"bg_url":"http://www.baidu.com",
}
p1 = 'dateP|order_date|8||||0-9|'
p2 = '|order_id|32|||||'
p3 = 'nullP|level2_mer_id||||||'
p4 = '|group_id|10|1|||0-z-Z|'
p5 = '|number|1|1|||0-9|'
p6 = '|file_suffix|10|||||'
list_p_d = []
list_p_d.append(p1)
list_p_d.append(p2)
list_p_d.append(p3)
list_p_d.append(p4)
list_p_d.append(p5)
list_p_d.append(p6)
ctcd = CreateTestCaseData(params_dict1, list_p_d)
# result = ctcd.produce_test_data(p1)
# for r in result:
# print(r)
# order_date, order_id, level2_mer_id, group_id, image_path, file_suffix, file_id, number, mer_bg_url, authorization_info
result = ctcd.produce_test_data()
with open('E:\\3.txt','w',encoding='utf-8') as f:
for r in result:
rr = r['order_date'] + '|' + r['order_id'] + '|' + r['level2_mer_id'] + '|' + r['group_id'] + '|' + r['image'] + '|' + r['file_suffix'] + '|' + r['file_id'] + '|' + r['number'] + '|' + r['mer_bg_url'] + '|' + r['authorization_info']
f.write(rr + "\n")
print(r)
python--生成测试数据的更多相关文章
- 用Python生成测试数据
转载自:http://blog.csdn.net/caz28/article/details/45972109 有时我们需要大量的数据,一般编个程序生成一堆随机数据,Python很适合文本处理,干这个 ...
- Python生成测试数据-Faker(非LOL选手-李相赫)
Faker介绍 官方文档:https://faker.readthedocs.io/en/master/ Faker is a Python package that generates fake d ...
- python生成excel测试数据
在功能测试时,经常会测到excel文件导入导出的功能,导入与导出时,需要测试系统单次导入大批量数据时是否正常, 如果系统承受不起太大的数据导入,则需要开发限制单次导入的数量,以防止系统服务异常.大量的 ...
- 通过 Python_Faker 生成测试数据
通过 Python_Faker 生成测试数据 一.介绍 在软件需求.开发.测试过程中,有时候需要使用一些测试数据,针对这种情况,我们一般要么使用已有的系统数据,你不可能通过手工来生成(最傻的方法)可能 ...
- 使用Hypothesis生成测试数据
Hypothesis是Python的一个高级测试库.它允许编写测试用例时参数化,然后生成使测试失败的简单易懂的测试数据.可以用更少的工作在代码中发现更多的bug. 安装 pip install hyp ...
- JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决
JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...
- db:seed 更好的生成测试数据
make:model -m -> 在database/migrations/目录下生成的table表中设置表的字段名和字段类型->在app/目录下对应的模型文件中设置可添加字段 -> ...
- Powerdesigner中如何生成测试数据
设计表完成以后,我们需要生成一些测试数据,可以直接更新到数据库中,下面我们就来试试: 第一步:建立需要的Profiles测试文件,[Model]--[Test Data Profiles],如图所示: ...
- 【python】【转】Python生成随机数的方法
如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...
- Python生成随机数的方法
这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...
随机推荐
- 跑酷游戏的一些bug总结(滥用FixedUpdate的坑)
最近把1年前的跑酷游戏demo拿出来重做了一遍,解决了2个之前的遗留bug. 虽然罪魁祸首都是FixedUpdate,但细节又不太一样.这里记录一下 1.点击空格键,角色会跳跃.而有时会跳的比之前高很 ...
- FPGA设计经验谈 —— 10年FPGA开发经验的工程师肺腑之言
FPGA设计经验谈 —— 10年FPGA开发经验的工程师肺腑之言 2014年08月08日 14:08 看门狗 关键词: FPGA 作者:friends 从大学时代第一次接触FPGA至今已有10多 ...
- [svc]influxdb+grafana实战-各省份api访问成功率统计
简单说下需求: 统计各个省份的 3大运营商的接口访问成功率,绘图展示 数据格式 {"mobile" : "15812345608", "provinc ...
- 线程相关函数(4)-pthread_mutex_lock(), pthread_mutex_unlock() 互斥锁
互斥锁实例: #include <pthread.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int pthread_mutex ...
- Java类的连接与初始化 (及2013阿里初始化笔试题解析)
Java虚拟机通过装载.连接.初始化来使得一个Java类型可以被Java程序所使用,如下图所示,其中连接过程又分为验证.准备.解析三个部分.其中部分类的解析过程可以推迟到程序真正使用其某个符号引用时再 ...
- struts2内置拦截器和自定义拦截器详解(附源码)
一.Struts2内置拦截器 Struts2中内置类许多的拦截器,它们提供了许多Struts2的核心功能和可选的高级特 性.这些内置的拦截器在struts-default.xml中配置.只有配置了拦截 ...
- 根据返回值动态加载select
// 路由 if (return_routeChoice != null && return_routeChoice != "") { for (var i = 0 ...
- lua工具库penlight--07函数编程(二)
列表压缩 列表压缩是以紧凑的方式通过指定的元素创建表.在 Python里,你可以说: ls = [x for x in range(5)] # == [0,1,2,3,4] 在 Lua,使用pl.c ...
- OpenCV使用标定图
本文由 @lonelyrains 出品,转载请注明出处. 文章链接: http://blog.csdn.net/lonelyrains/article/details/46915705 上一步生成标 ...
- 百度JS模板引擎
1. 应用场景 前端使用的模板系统 或 后端Javascript环境发布页面 2. 功能描述 提供一套模板语法,用户可以写一个模板区块,每次根据传入的数据,生成对应数据产生的HTML片段,渲染不同 ...