pytest结合allure

Allure

  • 一、文档
  • 二、指令
  • 三、装饰器
    • [3.1 @allure.step装饰器](#3.1 @allure.step装饰器)
    • [3.2 @allure.description装饰器](#3.2 @allure.description装饰器)
    • [3.3 @allure.title装饰器](#3.3 @allure.title装饰器)
    • [3.4 @allure.link、@allure.issue 和 @allure.testcase装饰器](#3.4 @allure.link、@allure.issue 和 @allure.testcase装饰器)
    • [3.5 @allure.epic、@allure.feature 和 @allure.story装饰器](#3.5 @allure.epic、@allure.feature 和 @allure.story装饰器)
    • [3.6 @allure.severity装饰器](#3.6 @allure.severity装饰器)

一、文档

allure文档

二、指令

1、运行测试并生成 allure 数据

python 复制代码
pytest --alluredir=./allure_results

2、生成并打开报告

python 复制代码
allure serve ./allure_results

三、装饰器

3.1 @allure.step装饰器

将函数或方法标记为测试步骤,并在 Allure 报告中展示步骤层级。

直接在测试方法或函数上添加 @allure.step 装饰器,并可自定义步骤名称,支持使用{}占位符。

python 复制代码
import allure

@allure.step("打开应用首页")
def open_homepage():
    print("-----------open--------------")
    pass

# 使用 {} 占位符将变量嵌入步骤名称,参数自动填充。
@allure.step("输入用户名和密码: {username},{password}")
def input_username(username, password):
    print(f"-----------{username}--------------")
    pass

# 步骤可以嵌套,形成清晰的逻辑层级
def test_login():
    open_homepage()
    input_username("test_user","1234")

conftest.py与@allure.step结合,显示单独的"前置/后置"树,用于配置初始化和销毁。

conftest.py

python 复制代码
import pytest
import allure

@allure.step("step in conftest.py for setup")
def conftest_test():
    pass

@allure.step("step in conftest.py for teardown")
def conftest_test_of_teardown():
    pass

@pytest.fixture(autouse=True)
def fixture_with_conftest_test():
    conftest_test()
    yield
    conftest_test_of_teardown()

运行结果:

3.2 @allure.description装饰器

为测试用例添加详细描述

1、@allure.description提供描述字符串

2、@allure.description_html添加html格式的描述

3、仅从测试方法的文档字符串获取描述

python 复制代码
import pytest
import allure

def test_unicode_in_description():
    """
    unicode描述使用不同的国家语言
    hello
    こんにちは
    你好伙计
    """
    assert 42 == int(6 * 7)


@pytest.mark.parametrize("username", ["user1", "user2"])
@allure.description("测试不同用户名的登录兼容性:username={username}")
def test_login_compatibility(username):
    pass

@allure.description_html("""
<h1>添加html格式的描述</h1>
""")
def test_description_html():
    pass


@allure.description("动态描述,可替换开始的描述")
def test_login_change():
    pass
    allure.dynamic.description("测试用例执行完了,更改描述")

3.3 @allure.title装饰器

使测试标题更具可读性,标题支持占位符并支持动态替换

python 复制代码
import pytest
import allure

@allure.title("@allure.title使测试标题更具可读性,标题支持占位符并支持动态替换")
def test_with_a_title():
    assert 2 + 2 == 4


@pytest.mark.parametrize("param1,param2,expected", [(1, 1, 2), (1, 3, 5)])
@allure.title("标题包含动态参数:{param1},{param2}")
def test_with_parametrize_title(param1, param2, expected):
    assert param1 + param2 == expected

@allure.title("动态描述,可替换开始的描述")
def test_title_update():
    pass
    allure.dynamic.title("测试用例执行完了,更改标题")

运行结果:

3.4 @allure.link、@allure.issue 和 @allure.testcase装饰器

用于在Allure报告中添加可点击的链接,提升测试报告的可追溯性。其中@allure.link表示通用资源链接,@allure.issue关联问题追踪系统(Bug/任务),@allure.testcase关联测试用例管理系统

python 复制代码
@allure.link("https://github.com/youngyangyang04/leetcode-master", name="项目仓库")
def test_example_for_link():
    pass

@allure.issue("PROJECT-123", "登录失败问题")
def test_login_for_issue():
    # 假设配置了issue链接模板:https://jira.company.com/browse/{issue}
    # 报告中将自动生成完整链接。
    pass

@allure.testcase("TC-456", "登录功能验证")
def test_login_for_testcase():
    # 假设配置了testcase链接模板:https://testrail.company.com/index.php?/cases/view/{testcase}
    pass

运行结果:

3.5 @allure.epic、@allure.feature 和 @allure.story装饰器

Epic、Feature、Story通常用于行为驱动开发(BDD)中的分层结构。Epic是最大的范围,通常对应大的业务目标;Feature是Epic下面的功能模块;Story则是更小的用户故事或功能点。Tag可能更灵活,用于标记测试的其他属性,比如优先级、模块等。

注解 层级定位 典型用途
@allure.epic 最高层级,表示宏观业务目标或大型项目模块。 描述系统级别的业务需求或长期目标。
@allure.feature 中间层级,属于某个epic下的功能模块。 描述一个独立的功能模块或子系统。
@allure.story 最细层级,属于某个feature下的具体用户故事或功能点。 描述具体的用户场景或需求点(通常对应敏捷开发中的User Story)。
@allure.tag 独立标签,无固定层级,灵活标记任意属性。 补充标记测试的附加属性(如优先级、测试类型、模块等)。
python 复制代码
import allure

@allure.epic("电商平台")
@allure.feature("订单管理")
@allure.story("用户取消订单")
@allure.tag("smoke", "high-priority")
def test_order_cancellation():
    pass

@allure.feature("feature_1")
def test_example_for_feature():
    pass


@allure.story("story_1")
def test_example_for_story():
    pass

@allure.feature("feature_2")
@allure.story("story_1")
def test_example_mix_feature_and_story():
    pass


@allure.feature("feature_2")
@allure.story("story_2")
def test_example_mix_feature_and_story_2():
    pass

可以使用如下命令行选项指定不同的测试集(可有多个,以逗号分隔

python 复制代码
--allure-epics xx1,xx2
--allure-features xx1,xx2
--allure-stories xx1,xx2

如:

python 复制代码
pytest --alluredir=./allure_results --allure-epics "电商平台"
pytest --alluredir=./allure_results --allure-stories story_1,story_2
pytest --alluredir=./allure_results --allure-features feature_2 --allure-stories story_2 #--allure-features 与 --allure-stories 是"或"关系,满足其一即被执行

3.6 @allure.severity装饰器

用于按严重性级别标记测试用例级别

python 复制代码
import allure

@allure.severity(allure.severity_level.BLOCKER)
def test_example_for_severity():
    pass

@allure.severity(allure.severity_level.CRITICAL)
def test_example_for_severity():
    pass

其中severity_level的枚举值如下:

python 复制代码
class Severity(str, Enum):
    BLOCKER = 'blocker'
    CRITICAL = 'critical'
    NORMAL = 'normal'
    MINOR = 'minor'
    TRIVIAL = 'trivial'

可以使用如下命令行选项指定不同的测试集(可有多个,以逗号分隔

python 复制代码
--allure-severities blocker,critical

注意:这个级别的关系是"或"

相关推荐
挽风82111 小时前
Pytest自动化框架
自动化·pytest·模块测试
摸鱼仙人~2 天前
结合unittest和pytest进行虚拟数据库测试
数据库·windows·pytest
江梦寻4 天前
如何使用 Python+Flask+win32print 实现简易网络打印服务
开发语言·后端·python·flask·pytest·web3.py·win32
qq_白羊座4 天前
pytest框架 核心知识的系统复习
pytest
@TangXin4 天前
单元测试-pytest框架实践
自动化测试·单元测试·pytest
evelol75 天前
【pytest框架源码分析四】pluggy源码分析之hook执行
自动化·pytest
云泽野6 天前
Pytest之parametrize参数化
android·python·pytest
evelol76 天前
【pytest框架源码分析三】pluggy源码分析之hook注册调用流程
自动化·pytest
云泽野7 天前
【Pytest】setup和teardown的四个级别
pytest