最近 在做接口测试的时候,遇到如下问题:如何通过数据驱动去做批量接口测试呢,我们的测试数据放在哪里去维护?下面整理出相关点,供大家参考
1、如何维护接口测试数据:放在excel文件中,通过python openpyxl第三方库,读取并写回测试结果。
#1、编写测试用例,读取测试用例,并写回 #第三方库 openpyxl from openpyxl import load_workbook #打开工作簿 wb=load_workbook["test_datas.xlsx"] #定位表单sheet sheet=wb["info"] #获取单元格cell值 value=sheet.cell(1,2).value #修改单元格cell值 sheet.cell(2,2).value="new" #获取最大行值row、最大列值column max_row=sheet.max_row max_cow = sheet.max_column #保存工作簿
wb.save("test_datas.xlsx")
2、如何对RESTful接口进行,接口请求,并获取返回的json值。用request第三方库,直接把请求体放在json参数里,入参去请求。
#2、进行RESTful,接口请求
#requests
import requests global cookies url = "http://172.17.0.128:15124/mdm/public/query" headers = {'Content-Type': 'application/json'} datas1={"Request": {"Head": {"LicId": "","ContentType": "text/json","TranCode": "DeptInfo","ServiceVersion":"5.0.2","ContentEncoding": "","Timestamp": "2018-10-19 17:09:55.137","TransferType": "","SecurityContent": "","OrgId": "01","Callback": "","AppId": "HIS0311","Version": "1.1","SecurityPolicy": "","AppType": "PC","MessageId": "EA455467-A04A-4E73-8BE4-190078D3C258"} ,"Body": {"Page":1,"PageSize":30,"Params":[{"key":"deptCode","value":"T234"}]}}} #python自带json把字典格式转成json格式 res=requests.post(url,json=requestbody,headers=headers,cookies=cookies) res.json() res=requests.get(url,json=requestbody,headers=headers,cookies=cookies) res.json()
3、如何对Wbeservice接口,进行处理?直接通过suds.client的Client()方法,创建webService对象,查看里面的各类接口-->>
在去service调用方法--->>最后对sax.text格式的返回值,用json.load(escape(res))转化成字典格式,
#3、进行Webservice,接口请求 #suds from suds.client import Client import json from xml.sax.saxutils import escape url = "http://172.17.0.128:15124/mdm/public/ws/MDMService?wsdl" operetions=“register” msg = '''{"Request":{"Head":{"LicId":"","ContentType":"text/json","TranCode":"hostipal","ServiceVersion":"1.0","ContentEncoding":"","Timestamp":"2018-10-1917:09:55.137","TransferType":"","SecurityContent":"","OrgId":"42504942400","Callback":"","AppId":"CIS","Version":"1.0","SecurityPolicy":"","AppType":"PC","MessageId":"EA455467-A04A-4E73-8BE4-190078D3C258"},"Body":{"Page":1,"PageSize":30,"Params":[{"key":"name","value":"dsnvb","like":1}]}}}''' # 创建一个webservice对象,来调用webservice里面的各类接口 c=Client(url) # print(c) # .service去调用接口函数方法method, 注:直接把xml参数当成一个字符串来传递就OK # 注意SoapUI:xml的格式需要将xml外围增加<![CDATA[xml]]> # 注册接口、查询接口、查询调阅接口 #接口返回类型: <class 'suds.sax.text.Text'> if operetions=='register': result=c.service.register(msg) elif operetions=='query': result=c.service.query(msg) else: result=c.service.querySubscribe(msg) # escape转换成字符类型,json.loads(<json>)转成字典 result = json.loads(escape(result)) return result #factory,去查看参数情况 # register=c.factory.create("register") # print(register) query_msg = '''{"Request":{"Head":{"LicId":"","ContentType":"text/json","TranCode":"hostipal","ServiceVersion":"1.0","ContentEncoding":"","Timestamp":"2018-10-1917:09:55.137","TransferType":"","SecurityContent":"","OrgId":"42504942400","Callback":"","AppId":"CIS","Version":"1.0","SecurityPolicy":"","AppType":"PC","MessageId":"EA455467-A04A-4E73-8BE4-190078D3C258"},"Body":{"Page":1,"PageSize":30,"Params":[{"key":"name","value":"dsnvb","like":1}]}}}''' querySubscribe_msg='''{"Request":{"Head":{"LicId":"","ContentType":"text/json","TranCode":"hostipal","ServiceVersion":"1.0","ContentEncoding":"","Timestamp":"2018-10-1917:09:55.137","TransferType":"","SecurityContent":"","OrgId":"42504942400","Callback":"","AppId":"CIS","Version":"1.0","SecurityPolicy":"","AppType":"PC","MessageId":"EA455467-A04A-4E73-8BE4-190078D3C258"},"Body":{"Page":1,"PageSize":30,"Params":[{"key":"name","value":"dsnvb","like":1}]}}}''' res=t.webservice_suds(url,register_msg,"register") print(res) print(type(res))