pytest测试allure报告相关笔记

3 阅读1分钟
import requests
import pandas as pd
import pytest
import allure
#一,allure安装
# 1,安装 pip install allure-pytest
# 2,安装allure命令主体 https://github.com/allure-framework/allure2/releases
# 3,运行命令生成报告 pytest --alluredir=./allure-results (依赖java,没有先安装java)
# 4,启动allure generate allure-results -o allure-report --clean
# 二,常用注解
@allure.epic("项目名称")
@allure.feature("模块名称")
@allure.story("接口名称")
@allure.title("用例标题")
@allure.description("用例描述")
def test_login():
    with allure.step("步骤1:输入帐号"):
        assert 1 == 1
    with allure.step("步骤1:输入帐号"):
        assert 2 == 2

# 三,把接口请求 & 返回写进报告  下载查看

def readcsv():
    df = pd.read_excel("c.xlsx")
    return df.to_dict(orient="records")

@pytest.mark.parametrize("case", readcsv())
def test_cases(case):
    url=case["url"]
    method=case["method"].upper()
    params=case["params"]

    if method == "GET":
        resp=requests.get(url,params=params)
        with allure.step("发送请求"):
            #attach 里面要传字符串 attachment_type可设置其类型
            allure.attach(f"{resp.content}+请求参数+{resp.json()}")
            #打印一些数据信息
            allure.dynamic.description(f"参数:{resp.url}\n响应:{resp.json()}")
    elif method == "POST":
        resp=requests.post(url,json=params)
        with allure.step(f"发送请求{resp.json()}"):  #步骤里也可以打印信息
            #attach 里面要传字符串 attachment_type可设置其类型
            allure.attach(f"{resp.content}+请求参数+{resp.json()}")
            # 打印一些数据信息
            allure.dynamic.description(f"参数:{resp.url}\n响应:{resp.json()}")

    assert resp.status_code == int(case["except"])

# 四,完整可直接运行的 Demo
import allure
import pytest
import requests
@allure.epic("接口自动化测试项目")
@allure.feature("测试接口模块")
class TestDemoApi:

    @allure.story("测试GET接口")
    @allure.title("测试获取公共接口")
    def test_get_api(self):
        with allure.step("1. 准备请求地址"):
            url = "https://httpbin.org/get"

        with allure.step("2. 发送GET请求"):
            res = requests.get(url)

        with allure.step("3. 断言状态码=200"):
            assert res.status_code == 200

        with allure.step("4. 保存数据到报告"):
            allure.attach(str(res.json()), "响应结果", allure.attachment_type.JSON)