我们需要4个文件:
1. 234.xlsx # Excel表格数据
2. readexcel.py # 读取excel表格数据
3. requestexcel.py # 获取请求之后的code
4. testexcel.py # 使用allure生成测试报告
1. 234.xlsx
Excel表格数据,这里将第一个url改成错误的了,为了演示错误的url,会返回404状态码。
2. readexcel.py
from openpyxl import load_workbook # 导包,没有的话 pip install openpyxl
class UseExcel():
def get_TestExcel(self):
# 打开表
workbook = load_workbook(r'C:\Users\JinYan\Desktop\自动化代码\requeststext\datademo\234.xlsx')
# 定位表单
sheet = workbook['Sheet1']
print(sheet.max_row) #3 行
print(sheet.max_column) #3 列
test_data = []#把所有行的数据放到列表中
for i in range(2, sheet.max_row+1): # 将第一行的标题去除所以从2开始
sub_data = {}#把每行的数据放到字典中
for j in range(1, sheet.max_column+1):
sub_data[sheet.cell(1, j).value] = sheet.cell(i, j).value
test_data.append(sub_data)#拼接每行单元格的数据
return test_data
t = UseExcel()
f = t.get_TestExcel()
print(f)
输出:
3. requestexcel.py
获取请求之后的code
from requeststext.readdemo.readexcel import UseExcel # 导包读取readexcel.py
import requests
res = UseExcel()
rr = res.get_TestExcel()
class RequestsExcelClass():
def requestsExcel(self):
item = []
for excel_i in rr:
if excel_i['method'] == 'get':
code = requests.get(url=excel_i['url'],params=excel_i['params'])
item.append(code.status_code)
else:
code = requests.get(url=excel_i['url'], params=excel_i['params'])
item.append(code.status_code)
return item
req = RequestsExcelClass()
print(req.requestsExcel())
输出:
4. testexcel.py
import pytest,os
from requeststext.requestsDemo.requestexcel import RequestsExcelClass
res = RequestsExcelClass()
rr = res.requestsExcel()
class TestCless():
def test001(self):
for i in rr:
assert i == 200 # 断言比较返回的状态码
pytest.main(['--alluredir', 'report/result', 'testexcel.py'])
split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
os.system(split)
然后在生成的html文件中打开浏览器查看
一个错误,ojbk