您的位置:首页 > 其它

自动测试接口各key内容的返回结果

2017-11-03 08:52 267 查看
自动化测试各个json中各个key内容错误和为空时的返回结果。

# -*- coding:utf-8 -*-
import requests
import json
import time
import base64
import warnings
warnings.filterwarnings("ignore")
'''
放置两个一样原始内容的产品1json的文件yc.lic和yc1.lic在脚本目录

放置两个一样原始内容的产品2json文件fcg.lic和fcg1.lic在脚本目录
修改测试的base_url
'''
def licens1(file):

#产品1地址改这个
base_url = "https://XX"

url = base_url+"XXX"
data1 = requests.get(url,verify=False)
data1 = data1.text
token = json.loads(data1)
token = token['data']['token']
#print token
data = {'token':token}
files = {
#2.lic是base64加密的文件
"licenseFile": open(file,"rb")
}
url2 = base_url + "XX"
r = requests.post(url2,data=data,files=files,verify=False)
print r.text

def licens2(file):
#产品2地址改这个地址
base_url = "XX"

url = base_url+"XX"
post_data={"Content-Type":"application/x-www-form-urlencoded"}
data1 = requests.post(url,data=post_data,verify=False)
token = data1.text
#print token

data = {'token':token}
files = {
#2.lic是base64加密的文件
"licenseFile": open("2.lic","rb")
}
url2 = base_url + "XX"+token
#print url2
r = requests.post(url2,data=data,files=files,verify=False)
print r.text

def autoread(license,filename1,filename2):
cases = ['','test']
for case in cases:
with open(filename1,"rb") as f:
line = f.readline()
d = json.loads(line)
for key,value in d.items():
#print type(value)
if type(value) == dict:
for key1, value1 in value.items():
#print  (key1, 'value:',value1)
#value[key1] = case
with open(filename2,"rb") as ff:
line = ff.readline()
dd = json.loads(line)
dd[key][key1] = case
with open("2.lic","wb") as f2:
f2.write(base64.b64encode(json.dumps(dd)))
if  case =="":
print u"%s为空的时返回结果:"%key1,
else:
print u"%s错误时返回结果:"%key1,
#print json.dumps(dd)
f2.close()
if license==1:
licens1("2.lic")
else:
licens2("2.lic")

else:
#print (key, 'value:',value)
with open(filename2,"rb") as ff:
line = ff.readline()
dd = json.loads(line)
dd[key] = case
with open("2.lic","wb") as f2:
f2.write(base64.b64encode(json.dumps(dd)))
if  case =="":
print u"%s为空的时返回结果:"%key,
else:
print u"%s错误时返回结果:"%key,
#print json.dumps(dd)
f2.close()
if license==1:
licens1("2.lic")
else:
licens2("2.lic")
if __name__ == '__main__':
print "******************产品1自动测试结果*******************************"
autoread(1,"yc.lic","yc1.lic")
print "******************产品2自动测试结果*******************************"
autoread(2,"fcg.lic","fcg1.lic")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  接口自动化