Python 中 Allure 测试报告框架的安装与使用指南

引言

Allure 是一个灵活轻量级的测试报告工具,它能够生成详细且富有洞察力的测试报告。在 Python 中,Allure 通常与 Pytest 结合使用,以提供更加丰富的测试结果展示。下面我将介绍关于如何在 Python 中使用 Allure 的详细操作。

一、环境准备

在开始之前,确保你的环境中已经安装了 Python 和 Pytest。接下来,你需要安装 Allure 的命令行工具和 allure-pytest 库。

安装 allure-commandline:

  1. 【下载地址】:https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.14.0/

  2. 下载zip完成之后,解压到本地电脑

  3. 进入bin目录,把bin目录加入Path环境变量

安装 Allure Python 库:

pip install allure-pytest

二、allure的使用方法

使用方法 参数值 参数说明
@allure.epic() epic描述 定义项目,当有多个项目使用,往下是Feature(特性)
@allure.feature() 模块名称 用例按照模块区分,有多个模块时给每个起名字
@allure.story() 用例名称 用例的描述
@allure.title() 用例标题 用例标题
@allure.testcase() 用例相关链接 自动化用例对应的功能存放系统的地址
@allure.issue() 缺陷地址 对应缺陷管理系统里边的缺陷地址
@allure.description() 用例描述 对测试用例的详细描述
@allure.step() 操作步骤 测试用例的操作步骤
@allure.severity() 用例等级 blocker、critical、normal、minor、trivial(用例的重要性等级)
@allure.link() 定义链接 用于定义一个需要在测试报告中展示的连接
@allure.attach() 添加附件 添加测试报告附件,如数据、文本、图片、视频、网页

三、编写测试用例

创建一个 Python 文件,比如 test_example.py,并编写以下代码:

import pytest
import allure

# 定义一个测试函数,使用 allure 的 feature 来标记测试的特性
@allure.feature("加法运算测试")
class TestAddition:
    # 使用 allure 的 story 来标记具体的测试场景
    @allure.story("测试两个正数相加")
    def test_add_positive_numbers(self):
        # 一个简单的断言
        assert 3 + 4 == 7

    # 使用 pytest 的 parametrize 来实现参数化测试 @pytest.mark.parametrize("x,y,expected", [(1, 2, 3), (3, 4, 7)])
    @allure.story("测试加法的参数化场景")
    def test_add_with_parameters(self, x, y, expected):
        # 参数化测试的断言
        assert x + y == expected

# 使用 allure 的 step 来记录测试步骤
@allure.step("执行加法操作")
def add_and_print_result(x, y):
    result = x + y
    print(f"The result of {x} + {y} is {result}")
    return result

# 测试用例中使用 allure step
def test_add_step(self):
    with allure.step("步骤1:输入两个参数"):
        x = 2
        y = 3

    result = add_and_print_result(x, y)
    with allure.step("步骤2:验证结果"):
        assert result == 5

四、运行测试

使用 Pytest 运行测试,并生成 Allure 报告:

pytest test_example.py --alluredir=allure-results

这条命令会执行 test_example.py 中的测试用例,并将 Allure 报告的生成结果存放在 allure-results 目录中。

五、生成 Allure 报告

  • 在命令行中使用 Allure 命令行工具生成报告:
  • allure serve allure-results
    

    执行该命令后,Allure 会启动一个本地服务器,并在默认的 Web 浏览器中打开生成的 Allure 报告。

  • 从结果生成报告:启动tomcat的服务,需要两步:生成报告、打开报告
  • 生成报告

    allure generate ./allure-results/ -o ./report/ --clean
    

    打开报告

    allure open  ./report_dir   #  ./report_dir 是报告所在的路径
    

    六、Allure 报告的定制化

    Allure 报告支持定制化,你可以通过添加不同的注解来丰富报告的内容。

    自定义描述

    使用 allure.description 来为测试用例添加描述:

    @allure.description("""
    这是一个复杂的测试用例,它执行多个步骤来验证登录流程。
    """)
    def test_complex_login(self):
        # ...
    

    附件

    你可以在测试用例中添加附件,比如截图或文本日志:

    def test_with_attachment(self):
        with allure.step("生成报告并附加日志"):
            allure.attach("这是日志文件的内容", body="文本内容", attachment_type=allure.attachment_type.TEXT)
    

    六、总结

    Allure 是一个功能强大的测试报告工具,它能够提供清晰、易于理解的测试结果。通过上述步骤,你可以在 Python 中轻松地使用 Allure 来增强你的测试报告。

    作者:blues_C

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python 中 Allure 测试报告框架的安装与使用指南

    发表回复