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--生成测试数据的更多相关文章

  1. 用Python生成测试数据

    转载自:http://blog.csdn.net/caz28/article/details/45972109 有时我们需要大量的数据,一般编个程序生成一堆随机数据,Python很适合文本处理,干这个 ...

  2. Python生成测试数据-Faker(非LOL选手-李相赫)

    Faker介绍 官方文档:https://faker.readthedocs.io/en/master/ Faker is a Python package that generates fake d ...

  3. python生成excel测试数据

    在功能测试时,经常会测到excel文件导入导出的功能,导入与导出时,需要测试系统单次导入大批量数据时是否正常, 如果系统承受不起太大的数据导入,则需要开发限制单次导入的数量,以防止系统服务异常.大量的 ...

  4. 通过 Python_Faker 生成测试数据

    通过 Python_Faker 生成测试数据 一.介绍 在软件需求.开发.测试过程中,有时候需要使用一些测试数据,针对这种情况,我们一般要么使用已有的系统数据,你不可能通过手工来生成(最傻的方法)可能 ...

  5. 使用Hypothesis生成测试数据

    Hypothesis是Python的一个高级测试库.它允许编写测试用例时参数化,然后生成使测试失败的简单易懂的测试数据.可以用更少的工作在代码中发现更多的bug. 安装 pip install hyp ...

  6. JavaScript 解析 Django Python 生成的 datetime 数据 时区问题解决

    JavaScript 解析 Django/Python 生成的 datetime 数据 当Web后台使用Django时,后台生成的时间数据类型就是Python类型的. 项目需要将几个时间存储到数据库中 ...

  7. db:seed 更好的生成测试数据

    make:model -m -> 在database/migrations/目录下生成的table表中设置表的字段名和字段类型->在app/目录下对应的模型文件中设置可添加字段 -> ...

  8. Powerdesigner中如何生成测试数据

    设计表完成以后,我们需要生成一些测试数据,可以直接更新到数据库中,下面我们就来试试: 第一步:建立需要的Profiles测试文件,[Model]--[Test Data Profiles],如图所示: ...

  9. 【python】【转】Python生成随机数的方法

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

  10. Python生成随机数的方法

    这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...

随机推荐

  1. C#实现麦克风採集与播放

    在网络聊天系统中.採集麦克风的声音并将其播放出来.是最基础的模块之中的一个.本文我们就介绍怎样高速地实现这个基础模块. 一. 基础知识 有几个与声音採集和播放相关的专业术语必需要先了解一下,否则.后面 ...

  2. keepalived virtual_router_id 44

    在同一局或网内如果有多个keepalived 的话 virtuall_router_id 44 (不能相同,但同一对,是一定相同)

  3. CCCallFunc CCCallFuncN CCCallFuncND的区别和使用

    CCCallFunc CCCallFuncN CCCallFuncND都用来创建带有回调函数的动作,区别主要在于回调函数是否带有参数 CCCallFunc是执行对应的回调函数,其中回调函数不可带参数. ...

  4. ADT离线安装教程

    安装eclipse软件.安装后点击HELP菜单,找到下面的Install New Software并点击.   之后会弹出一个对话框,然后我们点击add,接下来弹出ADD对话框,然后我们再点击arch ...

  5. Python删除列表中元素

    Python中列表(list)是很常用的数据结构,删除列表中的元素有几种方法 列表的remove方法 lst = [1, 1, 3, 4] lst.remove(1) # lst->[1, 3, ...

  6. jquery ajax 提交form表单 以及django后台接受

    HTML <form id="project_file_upload" enctype="multipart/form-data" > <di ...

  7. echarts 百度图表

    手册说明 http://echarts.baidu.com/option.html 3.0版本加了很多新属性可以看以上链接  此文用的3.x版本 第一步 引入 <script src=" ...

  8. Xml解析之PULL解析 例2

    <?xml version="1.0" encoding="UTF-8"?> <books> <book id="100 ...

  9. Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!

    各位哥哥姐姐弟弟妹妹小伙伴们春节好! 2014年对于我们程序员很重要, 因为今年是 "码" 年! SS在此重要之年到来之际热力推出两大重要功能恭贺新春: 1. 浏览器视图 以前SS ...

  10. jquery.autocomplete 传参问题

    今天遇到一个问题,我需要传一个文本库的内容给后台,然后用autocomplete显示内容,开始觉得很简单 不就是 function SalePrice_Complete(text) { //分割拿到索 ...