一、利用excel文档编写测试用例,通过python脚本江excel用例读取作为接口参数值,去请求接口,请求成功后会在服务生成.json文件。
import requests,xlrd import json,random #请求Go对话处理接口 def qndialog(data): url = "http://ip:port/qnDialog" headers = { 'Content-Type':'application/json' } response = requests.post(url=url, headers=headers, data=json.dumps(data)) result = response.json() return result if __name__ =="__main__": filenum = [] filename_1 = 'file/NLP.xlsx' bk = xlrd.open_workbook(filename_1) casename = ['Case01','Case02','Case03','Case04','Case05','Case06','Case07','Case08','Case09','Case10','Badcase01','Badcase02','Badcase03','Badcase04','Badcase05'] #casename = ['Case01'] for i in casename: sh = bk.sheet_by_name(i) print(sh.cell_value(1,7)) num = sh.nrows #print(result) #传参 data = { "enterpriseId": "2000000002", "callList": { "recordId": "55522111", "initialagentId": "18242307249", "curuserdn": "013726530769", "talkertype": "02418242307249", "begintime": "20200905111230", "endtime": "20200905111330", "agentID": "85731928", "callCentraTag": "ccod", "isEnd": False }, "taskData": { "bps": 0, "eps": 0, "index": 15, "callType": "2", "text": "嗯嗯,什么事", "silence_duration": 600, "speech_rate": 100, "emotion_value": "neutral" } } recordid = str(random.randint(110000000, 999999999)) filenum.append(recordid) #print(recordid) bps = 0 eps = 500 #result = 0 for i in range(num): if sh.cell_value(i,7) == '客户': calltype = "1" else: calltype = "2" data["callList"]["agentID"] = '85731928' data["callList"]["recordId"] = recordid data["taskData"]["callType"] = calltype data["taskData"]["text"] = sh.cell_value(i,4) data["taskData"]["index"] = i data["taskData"]["bps"] = bps data["taskData"]["eps"] = eps data["callList"]["isEnd"] = bool(sh.cell_value(i,8)) bps = eps + 500 eps = bps + 500 print(filenum) print(qndialog(data))
二、通过第一步生成.json文件,江文件放在指定本地指定目录下,解析.json文件,提取想要的结果数据,再结合excel用例文件,将用例+测试结果拼接起来写入新的excel文档中。
# coding=utf-8 import json,xlrd from openpyxl import load_workbook class OperationJson: def __init__(self, file_name=None): if file_name: self.file_name = file_name else: self.file_name = '8841566.json' self.data = self.get_data() def get_data(self): fp = open(self.file_name,encoding='UTF-8') data = json.load(fp) fp.close() return data["checkResults"] def get_value(self, id): return self.data[id] def xlrd_read(casename): filename_1 = 'file/NLP.xlsx' bk = xlrd.open_workbook(filename_1) sh = bk.sheet_by_name(casename) return sh if __name__ == '__main__': num = 0 casename = ['Case01','Case02','Case03','Case04','Case05','Case06','Case07','Case08','Case09','Case10','Badcase01','Badcase02','Badcase03','Badcase04','Badcase05'] filenum = ['972898005', '676691731', '130784914', '761151316', '668260302', '647986707', '270412005', '272212006', '141623284', '727904634', '623635355', '832728470', '922045347', '487408271', '877844092'] wb = load_workbook("NLP_result.xlsx") for m in wb.sheetnames: ws = wb[m] print(m) filename = 'file/{}.json'.format(filenum[num]) opers = OperationJson(filename) checkResults = opers.get_data() result = xlrd_read(casename[num]) for n in range(result.nrows): content = result.cell_value(n,4) ruleId = result.cell_value(n,3) audit_param = result.cell_value(n, 2) audit_object = result.cell_value(n, 1) role = result.cell_value(n, 7) ws.cell(row=n+1, column=1).value = audit_object ws.cell(row=n+1, column=2).value = audit_param ws.cell(row=n+1, column=3).value = ruleId ws.cell(row=n+1, column=4).value = content ws.cell(row=n+1, column=5).value = role for i in range(len(checkResults)): if checkResults[i]['content'] == content: rules = checkResults[i]['rules'] for j in range(len(rules)): if rules[j]['ruleId'] == ruleId: result_nlp = '成功:' + rules[j]['ruleId'] ws.cell(row=n+1, column=6).value = result_nlp print("成功:%s" %rules[j]['ruleId']) else: pass else: pass num += 1 wb.save("NLP_result.xlsx") wb.close()
总结:在日常工作涉及到接口测试,都可以编写测试脚本来提高测试效率,节省测试时间。