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

自动化测试各个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")


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值