Pytest学习笔记12-allure特性·@allure.step()和allure.attach
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
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01