优化之后的代码如下:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/01
# @Author : Laopi
import json
import logging
import allure
from testcase.conftest import *
baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
case_path = os.path.join(root_path, 'testdata')
os.chdir(case_path) # 修改工作路径
workbook = openpyxl.load_workbook('接口测试用例.xlsx') # 返回一个workbook数据类型的值
sheets = workbook.sheetnames
def get_token():
baseUrl = GetYamlData(ConfigHandler.config_path).get_yaml_data()
url = baseUrl['host']+"/login"
headers={"Content-Type":"application/x-www-form-urlencoded"}
data={"username":"admin","password":"123456"}
response = requests.post(url=url,
headers=headers,
data=data)
return response.json()['Admin-Token']
listdemo =[]
for i in range(len(sheets)):
workbook._active_sheet_index=i
sheet = workbook.active
for j in range(6,sheet.max_row+1):
case_name = sheet.cell(j, 1).value
listdemo.append([i,j,case_name])
# listdemo= [[0, 6]]
class TestCases:
@pytest.mark.parametrize("i,j,casename", listdemo)
@allure.title("{casename}")
def test_CRM(self,i,j,casename):
cookie = get_token()
url = baseUr