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

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

相关推荐
努力搬砖的咸鱼1 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)3 天前
PyTest框架学习
学习·pytest
程序员杰哥3 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥4 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻4 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿4 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder5 天前
Pytest Fixture 详解
数据库·pytest
not coder6 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂6 天前
(1)pytest简介和环境准备
pytest
not coder6 天前
Pytest Fixture 是什么?
数据库·oracle·pytest