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)

posted on 2018-02-04 16:41  我要的明天  阅读(384)  评论(0编辑  收藏  举报

导航