接口测试作业doExcel2
1 import xlrd 2 import time 3 from xlutils import copy 4 import json 5 import requests 6 7 # 打开excel,获取行数 8 worksheet = xlrd.open_workbook('../../data/教管系统-测试用例V1.2.xls').sheet_by_index(2) 9 nrows = worksheet.nrows # 获取行数 10 11 # 复制excel 12 workBook = xlrd.open_workbook('../../data/教管系统-测试用例V1.2.xls', formatting_info=True) 13 workBook_new = copy.copy(workBook) 14 worksheet_new = workBook_new.get_sheet(2) 15 16 # 登录,以及获取sessionid传给各请求 17 dict1 = {"Content-Type": "application/x-www-form-urlencoded"} 18 payload = { 19 "username": "auto", 20 "password": "sdfsdfsdf" 21 } 22 r = requests.post('http://localhost/api/mgr/loginReq', headers=dict1, data=payload) 23 sessionid = r.cookies['sessionid'] 24 cookie = {"sessionid": sessionid} 25 26 # 遍历原excel每一行,取出各元素,发送请求,将结果写入复制后的excel 27 for i in range(1, nrows): 28 content = json.loads(worksheet.row_values(i)[5]) 29 code = json.loads(worksheet.row_values(i)[6]) 30 code = code['code'] 31 url = worksheet.row_values(i)[9] 32 req_method = worksheet.row_values(i)[10] 33 header = json.loads(worksheet.row_values(i)[11]) 34 if req_method == 'post': 35 if worksheet.row_values(i)[1] == '增加课程': 36 randomStr = str(int(time.time() * 10000)) 37 name = content["name"].replace('{{courseName}}', randomStr) 38 desc = content["desc"] 39 display_idx = content["display_idx"] 40 payload = { 41 'action': 'add_course', 42 'data': f'{{"name": "{name}", "desc": "{desc}", "display_idx": "{display_idx}"}}' 43 } 44 r = requests.post(url, headers=header, data=payload, cookies=cookie) 45 retcode = r.json()["retcode"] 46 if retcode == code: 47 worksheet_new.write(i, 7, 'PASS') 48 else: 49 worksheet_new.write(i, 7, 'FAIL') 50 if 'reason' in r.json().keys(): 51 reason = r.json()["reason"] 52 worksheet_new.write(i, 8, reason) 53 54 elif req_method == 'get': 55 if worksheet.row_values(i)[1] == '列出课程': 56 action = content["action"] 57 pagenum = content["pagenum"] 58 pagesize = content["pagesize"] 59 payload = { 60 'action': action, 61 'pagenum': pagenum, 62 'pagesize': pagesize, 63 } 64 r = requests.get(url, headers=header, params=payload, cookies=cookie) 65 retcode = r.json()["retcode"] 66 if retcode == code: 67 worksheet_new.write(i, 7, 'PASS') 68 else: 69 worksheet_new.write(i, 7, 'FAIL') 70 if 'reason' in r.json().keys(): 71 reason = r.json()["reason"] 72 worksheet_new.write(i, 8, reason) 73 elif req_method == 'delete': 74 if worksheet.row_values(i)[1] == '删除课程': 75 action = content["action"] 76 c_id = content["id"] 77 payload = { 78 'action': action, 79 'id': c_id, 80 } 81 r = requests.delete(url, headers=header, data=payload, cookies=cookie) 82 retcode = r.json()["retcode"] 83 if retcode == code: 84 worksheet_new.write(i, 7, 'PASS') 85 else: 86 worksheet_new.write(i, 7, 'FAIL') 87 if 'reason' in r.json().keys(): 88 reason = r.json()["reason"] 89 worksheet_new.write(i, 8, reason) 90 91 # 保存复制后的excel 92 workBook_new.save(r'../../report/测试结果20200428.xls')
这里没有做任何封装,用for循环完成,有时间可以把登录、excel操作、课程功能、发送请求进行模块封装存到lib包中;
测试用例格式如下:
我们复制一个新的文档做测试结果填入,这里可以考虑引入unittest、pytest框架;
其中,增加课程自行添加action参数,其它与课上基本讲解相同。