http_request2.py用于发起http请求
#读取多条测试用例#1、导入requests模块import requests#从 class_12_19.do_excel1导入read_data函数from do_excel2 import read_datafrom do_excel2 import write_datafrom do_excel2 import count_case#定义http请求函数COOKIE=Nonedef http_request2(method,url,data):if method=='get':print('发起一个get请求')result=requests.get(url,data,cookies=COOKIE)else:print('发起一个post请求')result=requests.post(url,data,cookies=COOKIE)return result #返回响应体# return result.json() #返回响应结果:结果是字典类型:{'status': 1, 'code': '10001', 'data': None, 'msg': '登录成功'}#从Excel读取到多条测试数据sheets=['login','recharge','withdraw']for sheet1 in sheets:max_row=count_case(sheet1)print(max_row)for case_id in range(1,max_row):data=read_data(sheet1,case_id)print('读取到第{}条测试用例:'.format(data[0]))print('测试数据 ',data)#print(type(data[2]))#调用函数发起http请求result=http_request2(data[4],data[2],eval(data[3]))print('响应结果为 ',result.json())if result.cookies:COOKIE=result.cookies#将测试实际结果写入excel#write_data(case_id+1,6,result['code'])write_data(sheet1,case_id+1,7,str(result.json()))#对比测试结果和期望结果if result.json()['code']==str(data[5]):print('测试通过')#将用例执行结果写入Excelwrite_data(sheet1,case_id+1,8,'Pass')else:write_data(sheet1,case_id+1,8,'Fail')print('测试失败')
http_request2.pydo_excel2.py完成对excel中用例的读、写、统计
# 导入load_workbookfrom openpyxl import load_workbook#读取测试数据#将excel中每一条测试用例读取到一个列表中#读取一条测试用例——写到一个函数中def read_data(sheet_name,case_id):# 打开excelworkbook1=load_workbook('test_case2.xlsx')# 定位表单(test_data)sheet1=workbook1[sheet_name]print(sheet1)test_case=[] #用来存储每一行数据,也就是一条测试用例test_case.append(sheet1.cell(case_id+1,1).value)test_case.append(sheet1.cell(case_id+1,2).value)test_case.append(sheet1.cell(case_id+1,3).value)test_case.append(sheet1.cell(case_id+1,4).value)test_case.append(sheet1.cell(case_id+1,5).value)test_case.append(sheet1.cell(case_id+1,6).value)return test_case #将读取到的用例返回#调用函数读取第1条测试用例,并将返回结果保存在data中# data=read_data(1)# print(data)#将测试结果写会exceldef write_data(sheet_name,row,col,value):workbook1=load_workbook('test_case2.xlsx')sheet=workbook1[sheet_name]sheet.cell(row,col).value=valueworkbook1.save('test_case2.xlsx')#统计测试用例的行数def count_case(sheet_name):workbook1=load_workbook('test_case2.xlsx')sheet=workbook1[sheet_name]max_row=sheet.max_row #统计测试用例的行数return max_row
do_excel2.pytest_case2.xlsx存储测试用例