软件测试/测试开发丨学习笔记Allure2添加用例标题、用例步骤

本文为霍格沃兹测试开发学社学员学习笔记分享

原文链接:https://ceshiren.com/t/topic/24779

1、Allure2报告中添加用例标题

Allure 用法

方法名方法参数参数说明
@allure.epic()epic 描述敏捷里面的概念,定义史诗,往下是 feature
@allure.feature()模块名称功能点的描述,往下是 story
@allure.story()用户故事用户故事,往下是 title
@allure.title(用例的标题)用例的标题重命名 html 报告名称
@allure.step()操作步骤测试用例的步骤
@allure.testcase()测试用例的链接地址对应功能测试用例系统里面的 case
@allure.issue()缺陷对应缺陷管理系统里面的链接
@allure.description()用例描述测试用例的描述
@allure.severity()用例等级blocker,critical,normal,minor,trivial
@allure.link()链接定义一个链接,在测试报告展现
@allure.attachment()附件报告添加附件

Allure2 报告中添加用例标题

应用场景:为了让生成的测试报告便于阅读,可以为每条用例添加一个便于阅读的标题(可以使用中文标题)。生成的报告展示用例时,就会以设置的标题名展示出来。

image

Allure2 报告中添加用例标题

  • 通过使用装饰器 @allure.title 可以为测试用例自定义一个可阅读性的标题。

  • allure.title 的三种使用方式:

    1. 直接使用 @allure.title 为测试用例自定义标题。
    2. @allure.title 支持通过占位符的方式传递参数,可以实现测试用例标题参数化,动态生成测试用例标题。
    3. allure.dynamic.title 动态更新测试用例标题。

Allure2 报告直接设置标题

  • 方法一:直接使用装饰器。
import allure
import pytest

@allure.title("自定义测试用例标题")
def test_with_title():
    assert True

Allure2 报告参数化设置用例标题

  • 方式二:通过占位符的方式传递参数,可以实现测试用例标题参数化,动态生成测试用例标题。
import allure
import pytest

@allure.title("参数化用例标题:参数一:{param1} ,参数二: {param2}")
@pytest.mark.parametrize("param1, param2, expected", [
    (1, 1, 2),
    (0.1, 0.3, 0.4)
])
def test_with_parametrize_title(param1, param2, expected):
    assert param1 + param2 == expected

Allure2 报告动态更新测试用例标题

  • 方式三:动态更新测试用例标题。
@allure.title("原始标题")
def test_with_dynamic_title():
    assert True
    allure.dynamic.title("更改后的新标题")

2、Allure2报告中添加用例步骤

Allure2 报告中添加用例步骤

应用场景:编写自动化测试用例的时候经常会遇到需要编写流程性测试用例的场景,一般流程性的测试用例的测试步骤比较多,我们在测试用例中添加详细的步骤会提高测试用例的可阅读性。

Allure2 报告中添加用例步骤

  • Allure 支持两种方法:

    • 方法一:使用装饰器定义一个测试步骤,在测试用例中使用。
    • 方法二:使用 with allure.step() 添加测试步骤。

Allure2 报告装饰器添加用例步骤

  • 方法一:使用装饰器定义一个测试步骤,在测试用例中使用。
# 方法一:使用装饰器定义一个测试步骤,在测试用例中使用
import allure
import pytest

@allure.step
def simple_step1(step_param1, step_param2 = None):
    '''定义一个测试步骤'''
    print(f"步骤1:打开页面,参数1: {step_param1}, 参数2:{step_param2}")

@allure.step
def simple_step2(step_param):
    '''定义一个测试步骤'''
    print(f"步骤2:完成搜索 {step_param} 功能")

@pytest.mark.parametrize('param1', ["pytest", "allure"], ids=['search pytest', 'search allure'])
def test_parameterize_with_id(param1):
    simple_step2(param1)


@pytest.mark.parametrize('param1', [True, False])
@pytest.mark.parametrize('param2', ['value 1', 'value 2'])
def test_parametrize_with_two_parameters(param1, param2):
    simple_step1(param1, param2)

@pytest.mark.parametrize('param2', ['pytest', 'unittest'])
@pytest.mark.parametrize('param1,param3', [[1,2]])
def test_parameterize_with_uneven_value_sets(param1, param2, param3):
    simple_step1(param1, param3)
    simple_step2(param2)

Allure2 报告中添加用例步骤

  • 方法二:使用 with allure.step() 添加测试步骤。
# 方法二:使用 `with allure.step()` 添加测试步骤
@allure.title("搜索用例")
def test_step_in_method():
    with allure.step("测试步骤一:打开页面"):
        print("操作 a")
        print("操作 b")

    with allure.step("测试步骤二:搜索"):
        print("搜索操作 ")

    with allure.step("测试步骤三:断言"):
        assert True

image

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值