chap4-关联接口测试-通过全局变量传递cookie
Posted come202011
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了chap4-关联接口测试-通过全局变量传递cookie相关的知识,希望对你有一定的参考价值。
# 1 http_request.py import requests class HttpRequest: def http_request(self, url, method, data=None, cookie=None): try: if method.upper() == "GET": res = requests.get(url, data, cookies=cookie) elif method.upper() == "POST": res = requests.post(url, data, cookies=cookie) else: print("请输入正确的参数") except Exception as e: print("请求报错了:{}".format(e)) raise e return res if __name__ == ‘__main__‘: # 登录 login_url = "http://v.rainbowred.com/login" login_data = {"username": "15546355872", "password": "123456", "rememberCheck": "1", "loginStatus": "1", "rememberStatus": "1", "autoLogin": "0", "language": "zh"} login_res = HttpRequest().http_request(login_url, "post", login_data) print(login_res.json()) # 选择公司 c_url = "http://v.rainbowred.com/chc" c_data = {"companyId": "21"} c_res = HttpRequest().http_request(c_url, "post", c_data, cookie=login_res.cookies) print(c_res.json()) # 2. do_excel.py # 把数据从excel中读取出来 from openpyxl import load_workbook class DoExcel: def get_data(self, file, sheet): wb = load_workbook(file) sheet = wb[sheet] test_data = [] for i in range(2, sheet.max_row+1): row_data = {} # 数值类型读出来还是数值,不用使用函数eval转换 row_data["case_id"] = sheet.cell(i, 1).value row_data["url"] = sheet.cell(i, 2).value row_data["method"] = sheet.cell(i, 3).value row_data["data"] = eval(sheet.cell(i, 4).value) row_data["title"] = sheet.cell(i, 5).value row_data["expected"] = sheet.cell(i, 6).value test_data.append(row_data) return test_data def write_back(self, file, sheet, i, j, value): wb = load_workbook(file) sheet = wb[sheet] # 写入测试结果, value必须是数据或者字符串类型才能被写回,写入数据前必须关闭工作簿 sheet.cell(i, j).value = value # 保存工作簿 wb.save(file) if __name__ == ‘__main__‘: test_data = DoExcel().get_data("../test_data/test_data.xlsx", "company") print(test_data) # 3. run.py # 关联接口测试,通过全局变量传递cookie from API_AUTO.tools.http_request import HttpRequest from API_AUTO.tools.do_excel import DoExcel COOKIE = None def run(test_data): global COOKIE for item in test_data: print("正在测试的用例是:{}".format(item["title"])) res = HttpRequest().http_request(item["url"], item["method"], item["data"], cookie=COOKIE) if res.cookies: COOKIE = res.cookies print("测试的结果是:{}".format(res.json())) DoExcel().write_back("test_data/test_data.xlsx", "company", item["id"]+1, str(res.json())) test_data = DoExcel().get_data("test_data/test_data.xlsx", "company") run(test_data)
以上是关于chap4-关联接口测试-通过全局变量传递cookie的主要内容,如果未能解决你的问题,请参考以下文章