• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest学习笔记12-allure特性·@allure.step()和allure.attach

武飞扬头像
_黎晟
帮助2

allure.step()和allure.attach

前言

allure除了支持常用的pytest功能外( xfails、fixture、skips和skipif、parametrize),自身还有一些强大实用的功能。

本次主要介绍step和attach功能,可以设置测试步骤及添加报告中的附件。

@allure.step()
  • Allure 报告的最重要的方面是它允许对每个测试用例的调用进行非常详细的步骤说明
  • 通过@allure.step装饰器来实现
  • 可以存储在您的测试之外,并在需要时仅导入
  • Step 方法可以具有任意深度的嵌套结构
demo代码
# __File__   = test_01.py
import allure
from .steps import imported_step

@allure.step("step-1")
def passing_step():
    pass

@allure.step("step-2")
def step_with_nested_steps():
    nested_step()
    
@allure.step("step-3")
def nested_step():
    nested_step_with_arguments(1, 'abc')

@allure.step("step-4")
def nested_step_with_arguments(arg1, arg2):
    pass

def test_with_imported_step():
    passing_step()
    imported_step()

def test_with_nested_steps():
    passing_step()
    step_with_nested_steps()
学新通

执行结果如下:
学新通
学新通

使用总结
  • 参数:title,表示step的名称,可以直接展示在测试报告里
  • 特点:可以嵌套使用,也可以外部调用
  • 使用场景:主要用于场景用例的组织,使用多条功能点用例组合成场景用例(允许使用外部的用例)
allure.attach
  • 功能:测试报告可以显示许多不同类型的附件,这些附件可以补充测试、步骤或fixture结果
参数介绍
  • 用法1:allure.attach(body, name, attachment_type, extension),参数如下:
    • body:要写入附件的原始内容
    • name:附件名
    • attachment_type:allure.attachment_type类型之一
    • extension:文件的扩展名
  • 用法2:allure.attach.file(source, name, attachment_type, extension),参数:
    • source:文件路径(字符串)
    • (其他参数相同)

用法1用于没有想要导入的现成附件的情况,新增然后展示到测试报告内

用法2主要用于已有对应附件,可直接使用路径导入,展示到测试报告内

上代码

创建test_01.py文件,内容如下:

import allure
import pytest

@pytest.fixture
def attach_file_in_module_scope_fixture_with_finalizer(request):
    allure.attach('在fixture前置步骤中添加一个txt附件', 'fixture 前置附件', allure.attachment_type.TEXT)

    def finalizer_module_scope_fixture():
        allure.attach('在fixture后置步骤中添加一个txt附件', 'fixture 后置附件',
                      allure.attachment_type.TEXT)

    request.addfinalizer(finalizer_module_scope_fixture)

def test_with_attachments_in_fixture_and_finalizer(attach_file_in_module_scope_fixture_with_finalizer):
    pass

def test_multiple_attachments():
    allure.attach.file('chestnut_1f330.png', attachment_type=allure.attachment_type.PNG)
    allure.attach.file('allure_report.html', attachment_type=allure.attachment_type.HTML)
    allure.attach('<head></head><body> 一个HTML页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
学新通

查看运行结果如下:
学新通

可以看到前后置txt附件如上。
学新通

可以看到使用attach()插入图片和HTML附件及使用attach.file()导入一个已经存在的HTML。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgeakch
系列文章
更多 icon
同类精品
更多 icon
继续加载