Pytest allure excel 接口自动化框架


1、框架结构图

2、config 配置数据

#_data:2022/4/3
#_author:多测师_Evan 

import os

project_path = "D:\Evan_duoceshi\CodeFile\pytest_allure_excel"
excel_data_path = os.path.join(project_path, "data", "test_data.xls")

3、data 中数据配置

4、pubilc 中封装的几个函数

# readExcel 中封装了excel读取功能
# 安装第三方模块 xlrd -- 实现读取excel文件的
# pip install xlrd
import xlrd

def getValue(fileName, sheetName, apiName):
    book = xlrd.open_workbook(fileName)
    sheet = book.sheet_by_name(sheetName)
    rows = sheet.nrows  # 获取有效行数 6
    colvalues = sheet.col_values(0)
    valueList = []
    for i in range(1, rows):
        if colvalues[i] == apiName:
            rowvalue = sheet.row_values(i, 1, 7)
            valueList.append(tuple(rowvalue))
    return valueList

if __name__ == '__main__':
    print(getValue("../data/test_data.xls", "Sheet1", "cms_login"))
# request_func 中封装了请求操作
import requests
import allure

@allure.feature("封装的请求方法")
class ApiKey():

    def request_api(self, method, url, **kwargs):
        res = requests.request(method, url, **kwargs)
        return res

    @allure.step("发起get请求")
    def get_request(self, url, params=None, **kwargs):
        res = requests.get(url, params=params, **kwargs)
        return res

    @allure.step("发起post请求")
    def post_request(self, url, data=None, json=None, **kwargs):
        res = requests.post(url, data=data, json=json, **kwargs)
        return res
# assert_func 中简单封装了断言操作
import allure

@allure.step("断言测试结果")
def assert_result(check_value, Actual_result):
    assert check_value == Actual_result

5、test_cases 用例实现封装

import pytest
from public.request_func import *
from public.readExcel import *
from config.path_config import *
from public.assert_func import assert_result
import json

@allure.feature("cms_login_api")
class TestCmsLogin():

    @allure.story("cms_login_case")
    @pytest.mark.parametrize("data", getValue(excel_data_path, "Sheet1", "cms_login"))
    def test_cms_login(self,data):
        """
        0   用例标题title
        1   请求方式
        2   请求url
        3   请求头
        4   请求体
        5   预期值
        """
        allure.dynamic.title(data[0])   # 自定义报告中用例title
        res = ApiKey().post_request(url=data[2], headers=json.loads(data[3]), data=json.loads(data[4]))
        assert_result(res.json()["msg"], data[5])

if __name__ == '__main__':
    pytest.main(["-s", "./test_cms_login_case.py", "--alluredir=../result", "--clean-alluredir"])
    os.system("allure generate ../result -c -o ../report")

6、生成的可读性非常高的报告