day12接口自动化测试框架

1.jsonpath模块的使用

import jsonpath

#使用jsonpath模块可以很快获取json数据中的values的值
dic={
"error_code": 0,
"stu_info": [
{
"id": 2057,
"name": "xiaohei",
"sex": "nan",
"age": 29,
"addr": "beijing",
"grade": "tianxie",
"phone": "18712321234",
"gold": 100
}
]
}
#使用'..' 不管在第几层都能正常获取到,如果存在返回的结果类型是list,不存在key值返回False

s=jsonpath.jsonpath(dic,'$..name')

print(s)

2.预期结果分割
s1='error_code=0,name!=xiaohei,age>18'

seqs=['!=','>=','<=','>','<','=','in','not']

s1_list=s1.split(',')

dic=[]
for s in s1_list:
for seq in seqs:
if seq in s:
key,value=s.split(seq)
tmp=[key,seq,value]
dic.append(tmp)
break #找到符合条件的符号后直接停止内层循环

print(dic)
3.参数化请求主要是生成手机号、身份证、邮箱、时间戳,并返回字典格式的数据
#主要是生成手机号、身份证、邮箱号
import random
import string
import time
class ParseParam:
func_map=['phone','email','id_card','cur_time']
def __init__(self,param=None):
self.param=param

def phone(self):
phone_start=["130","131","132","155","156","186","134"
,'135','181','137']

start=random.choice(phone_start)

phone_end=random.randint(0,99999999)

end=str(phone_end).zfill(8)

return start+end

def email(self):

email_end=['163.com','qq.com','126.com','sina.com']

end=random.choice(email_end)
start_str='API_TEST_'
email_start=''.join(random.sample(string.ascii_uppercase+string.digits,6))

return start_str+email_start+end

#生成身份证的方法后续自己补
def id_card(self):
return '5652343242343434343'

#返回当前时间戳
def cur_time(self):

return int(time.time())
#使用生成的phone,id_card,email参数化
def parase(self):
for func in self.func_map:
tmp=str(getattr(self,func)())
# print(func,tmp,type(func),type(tmp))
self.param=self.param.replace('<%s>'%func,tmp)
return self.param

#获取请求数据生成字典
def strTodic(self):
data={}
parase=self.param.split('&')
for i in parase:
tmp=i.split('=')
# print(tmp)
if len(tmp)>1: #防止用例书写不规范导致出错
key,value=tmp
data[key]=value
return data
if __name__ == '__main__':
s='username=niuyang&passwd=aA123456&emai=<email>&phone=<phone>&id_card=<id_card>&cur_time=<cur_time>'
p=ParseParam(s)
# res=getattr(p,'email') #获取一个对象中的属性(方法、变量)
# print(res()
p.parase()
data=p.strTodic()
print(data)


生成身份证方法:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# print(BASE_DIR)
DC_PATH = os.path.join(BASE_DIR ,"distrcitcode.txt")
# print(DC_PATH)
# 随机生成身份证号
def getdistrictcode(self):
with open(DC_PATH,'r', encoding='UTF-8') as file:
data = file.read()
districtlist = data.split(' ')
for node in districtlist:
# print node
if node[10:11] != ' ':
state = node[10:].strip()
if node[10:11] == ' ' and node[12:13] != ' ':
city = node[12:].strip()
if node[10:11] == ' ' and node[12:13] == ' ':
district = node[14:].strip()
code = node[0:6]
codelist.append({"state": state, "city": city, "district": district, "code": code})
return codelist

def gennerator(self):
# codelist = []
if not codelist:
self.getdistrictcode()
id = codelist[random.randint(0, len(codelist))]['code'] # 地区项
id = id + str(random.randint(1930, 2013)) # 年份项
da = datetime.date.today() + datetime.timedelta(days=random.randint(1, 366)) # 月份和日期项
id = id + da.strftime('%m%d')
id = id + str(random.randint(100, 300)) # ,顺序号简单处理
i = 0
count = 0
weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] # 权重项
checkcode = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '5', '9': '3',
'10': '2'} # 校验码映射
for i in range(0, len(id)):
count = count + int(id[i]) * weight[i]
id = id + checkcode[str(count % 11)] # 算出校验码
return id




原文地址:https://www.cnblogs.com/zzzao/p/10017940.html