Allure 集成 pytest

Allure 是一个强大的测试报告工具,与 pytest 集成可以生成详细的测试报告,包括测试步骤、测试数据、截图、错误堆栈等。


1. 安装 Allure 和相关依赖

  1. 安装 pytest-allure-adaptor 插件:

    复制代码
    pip install allure-pytest
  2. 确保本地已安装 Allure 工具。如果未安装,可通过以下方式安装:

    • MacOS:

      复制代码
      brew install allure

    • Windows/Linux: 下载 Allure 的 官方版本,解压后配置到环境变量中。

  3. 验证安装是否成功:

    复制代码
    allure --version

2. pytest 与 Allure 的集成

1)生成 Allure 报告

确保已经运行了 pytest 测试,并生成了 Allure 数据文件。例如:

复制代码
pytest --alluredir=./allure-results

这里的 allure-results 目录是 Allure 生成的原始数据文件存储路径。


2) 使用 Allure 命令生成 HTML 报告

运行以下命令,将 allure-results 转换为 HTML 报告:

复制代码
allure generate ./allure-results -o ./allure-report --clean

-o ./allure-report 指定生成的 HTML 报告输出目录。

--clean 会在生成新报告前清除旧的报告内容。

3) 打开生成的 Allure 报告

  • 方式 1:通过 Allure 命令直接打开

    运行以下命令,Allure 会启动一个本地服务来展示报告:

    复制代码
    allure open ./allure-report

    执行后,浏览器会自动打开 Allure 报告。

    方式 2:手动打开 index.html

  • 进入生成的 Allure 报告目录:

复制代码
cd ./allure-report
  • 找到 index.html 文件,用浏览器直接打开:
    • Windows: 双击 index.html 文件。

    • Linux/Mac: 使用浏览器打开,例如:

      复制代码
      firefox index.html
      # 或
      google-chrome index.html
  • Allure 服务启动方式推荐:

    使用 allure open 命令启动本地服务查看报告。这种方式可以动态加载资源,避免直接打开 index.html 时某些功能(如图表、附件)因浏览器安全策略而失效。

  • 防止报错:

    如果直接打开 index.html 时出现资源加载失败的问题(如无法显示图表),建议使用以下命令启动本地 HTTP 服务:

    复制代码
    python -m http.server 8080

    然后在浏览器访问:
    http://localhost:8080/allure-report/index.html


3. Allure 的常用装饰器

Allure 提供了一系列装饰器,用于标记测试用例的元数据、步骤和测试附加信息。

3.1 测试标题和描述
  • @allure.title

    自定义测试用例标题。

    复制代码
    import allure
    
    @allure.title("测试用户登录功能")
    def test_login():
        assert login("user", "password") == "success"
  • @allure.description

    添加测试用例的详细描述。

    复制代码
    @allure.description("验证用户登录接口是否返回正确的结果")
    def test_login():
        assert login("user", "password") == "success"
3.2 测试分类
  • @allure.severity

    标记测试用例的优先级(blocker, critical, normal, minor, trivial)。

    复制代码
    @allure.severity(allure.severity_level.CRITICAL)
    def test_payment():
        assert process_payment() is True
  • @allure.feature

    按功能模块对测试用例进行分组。

    复制代码
    @allure.feature("登录模块")
    def test_login():
        assert login("user", "password") == "success"
  • @allure.story

    标记功能模块中的子功能。

    复制代码
    @allure.feature("登录模块")
    @allure.story("用户使用正确的凭据登录")
    def test_login():
        assert login("user", "password") == "success"
3.3 测试步骤
  • @allure.step
    添加测试步骤,便于报告中展示测试用例执行的流程。

    复制代码
    @allure.step("输入用户名和密码")
    def enter_credentials(username, password):
        pass
    
    @allure.step("点击登录按钮")
    def click_login():
        pass
    
    def test_login():
        enter_credentials("user", "password")
        click_login()
        assert True

3.4 附件
  • @allure.attach

    添加测试附件,可以是文本、HTML、图片等。

    复制代码
    @allure.attach("这是一个文本附件", name="文本信息", attachment_type=allure.attachment_type.TEXT)
    def test_attach_text():
        assert True
    
    @allure.attach("<h1>这是一个HTML附件</h1>", name="HTML信息", attachment_type=allure.attachment_type.HTML)
    def test_attach_html():
        assert True
  • 附加图片或日志:

    复制代码
    @allure.attach.file("screenshot.png", name="截图", attachment_type=allure.attachment_type.PNG)
    def test_attach_image():
        assert True

4. 结合案例:完整示例

复制代码
import allure

@allure.feature("支付模块")
@allure.story("支付功能验证")
@allure.severity(allure.severity_level.BLOCKER)
@allure.title("验证用户支付成功")
def test_payment():
    with allure.step("初始化支付数据"):
        payment_data = {"amount": 100, "currency": "USD"}
        allure.attach(str(payment_data), name="支付数据", attachment_type=allure.attachment_type.JSON)
    
    with allure.step("执行支付请求"):
        response = process_payment(payment_data)  # 假设这个是支付函数
        allure.attach(str(response), name="支付响应", attachment_type=allure.attachment_type.TEXT)

    with allure.step("校验支付结果"):
        assert response["status"] == "success", "支付失败"

def process_payment(data):
    # 模拟支付接口响应
    return {"status": "success", "transaction_id": "12345"}

5. Allure 的核心功能

  1. 多层次分组:

    • 使用 @allure.feature@allure.story 组织测试报告的层级。
  2. 测试用例管理:

    • 自定义标题、描述、标签(如优先级、用例类型)。
  3. 步骤可视化:

    • 通过 @allure.step 展示用例执行的具体过程。
  4. 附件支持:

    • 附加截图、日志、JSON 响应等信息。
  5. 失败快照:

    • 自动捕获失败的堆栈信息。

6. 总结

通过集成 Allure 和 pytest,可以实现清晰直观的测试报告展示。合理利用装饰器和附件功能,能够极大提升测试报告的可读性与信息量,使开发和测试团队更高效地协作与问题定位。

相关推荐
昔时扬尘处11 小时前
【Files Content Replace】文件夹文件内容批量替换自动化测试脚本
c语言·python·pytest·adi
我的xiaodoujiao16 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 32--开源电商商城系统项目实战--如何区分登录状态
python·学习·测试工具·pytest
昔时扬尘处18 小时前
如何检测python和pytest的安装环境
开发语言·python·pytest·自动化测试平台·adi
我的xiaodoujiao20 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 33--基础知识 8--切换窗口句柄
python·学习·测试工具·pytest
cliffordl3 天前
自动化测试(Pytest)
pytest
噔噔噔噔@4 天前
详细介绍Python+Pytest+BDD+Playwright,用FSM打造高效测试框架
开发语言·python·pytest
黑客思维者5 天前
Python自动化测试Pytest/Unittest深度解析与接口测试落地实践
开发语言·python·pytest·unittest
Mr_Xuhhh6 天前
接口自动化测试--requests模块
pytest
19226386 天前
Auto 3: 任意位置多目标点编队控制的分布式控制策略
pytest
我的xiaodoujiao8 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 31--开源电商商城系统项目实战--加入购物车、提交订单测试场景
python·学习·测试工具·pytest