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)
相关推荐
houmenghu 2020-11-17
kentrl 2020-11-10
逍遥友 2020-10-26
jincheng 2020-09-01
Blueberry 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
阳光之吻 2020-08-03
PkJY 2020-07-08
hzyuhz 2020-07-04
89407707 2020-06-27
服务器端攻城师 2020-06-26
阳光岛主 2020-06-25
笨重的蜗牛 2020-06-20
xuanwenchao 2020-06-14
Lophole 2020-06-13
明瞳 2020-06-12
songerxing 2020-06-11