需要执行命令来进行下载对应的文件
pip install requests
pip install pytest
pip install allure-pytest
1、我们需要在Excel文档中填写对应的接口和参数和提交方式,需要把接口文档放在data文件夹中
2、创建一个文件用来读取Excel
3、写入代码
from openpyxl import load_workbook
class UseExcel():def get_TestExcel(self):# 打开表workbook = load_workbook('../data/接口.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):sub_data = {}#把每行的数据放到字典中for j in range(1,sheet.max_column+1):sub_data[sheet.cell(1,j).value] = sheet.cell(i,j).valuetest_data.append(sub_data)#拼接每行单元格的数据return test_data
t = UseExcel()
f = t.get_TestExcel()
print(f)
4、我们需要创建用来请求的接口的文件
需要执行命令 pip install requests 下载对应的文件
import requests
from readdata.readdataxlsx import UseExcel
u = UseExcel()
lists = u.get_TestExcel()
litm = []
class Testxlsx:def testxlsx(self):for xlsx_i in lists:if xlsx_i.get('tijiao') == 'get':r = requests.get(url=xlsx_i['ual'],params=xlsx_i['sj'])litm.append(r.status_code)else:r = requests.post(url=xlsx_i['ual'],data=xlsx_i['sj'])litm.append(r.status_code)return litm
t = Testxlsx()
print(t.testxlsx())
5、创建我们的单元测试的文件,编写单元测试并且生成测试报告
import pytest,os
from test_case.requxlsx import Testxlsx
t = Testxlsx()
lists = t.testxlsx()
class Testxls:def testxlsx(self):for xlsx_i in lists:assert xlsx_i == 200if __name__ == '__main__':pytest.main(['--alluredir', 'report/result', 'test_xlsx.py'])split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'os.system(split)
6、执行成功会出现一个文件夹,我们点击index用浏览器进行访问,就可以看到我们的测试报告