Pytest测试报告工具Allure的高级用法
作者:小旭2021 发布时间:2023-06-20 17:21:09
Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。
1、严重性
如果你想对测试用例进行严重等级划分,可以使用@allure.severity装饰器,它可以应用于函数,方法或整个类。
它以allure.severity_level
枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),MINOR(轻微),TRIVIAL(不重要)。
示例:
# test_sample.py
import allure
# 两数相加
def add(x, y):
return x + y
# 测试类
@allure.severity(allure.severity_level.TRIVIAL)
class TestAdd:
@allure.severity(allure.severity_level.MINOR)
def test_first(self):
assert add(3, 4) == 7
@allure.severity(allure.severity_level.NORMAL)
def test_second(self):
assert add(-3, 4) == 1
@allure.severity(allure.severity_level.CRITICAL)
def test_three(self):
assert add(3, -4) == -1
@allure.severity(allure.severity_level.BLOCKER)
def test_four(self):
assert add(-3, -4) == -7
运行:
E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items
test_sample.py .... [100%]
=========================================================================== 4 passed in 0.06s ===========================================================================
报告:
你还可以通过--allure-severities
选项指定严重等级运行,多个以逗号分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-severities normal,critical
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items
test_sample.py .. [100%]
=========================================================================== 2 passed in 0.02s ===========================================================================
2、功能
如果你想对测试功能、测试场景进行行为描述,可以分别使用装饰器:@allure.feature
和@allure.story
。
示例:
# test_sample.py
import allure
# 两数相加
def add(x, y):
return x + y
@allure.feature('测试类')
class TestAdd:
@allure.story('01测试两个正数相加')
def test_first(self):
assert add(3, 4) == 7
@allure.story('02测试负数正数相加')
def test_second(self):
assert add(-3, 4) == 1
@allure.story('03测试正数负数相加')
def test_three(self):
assert add(3, -4) == -1
@allure.story('04测试两个负数相加')
def test_four(self):
assert add(-3, -4) == -7
运行:
E:\workspace-py\Pytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items
test_sample.py .... [100%]
=========================================================================== 4 passed in 0.06s ===========================================================================
报告:
你也可以通过--allure-features
和--allure-stories
选择指定具体功能和故事运行,多个以逗号分隔。
E:\workspace-py\Pytest>pytest test_sample.py --allure-stories 01测试两个正数相加
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:\workspace-py\Pytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items
test_sample.py . [100%]
=========================================================================== 1 passed in 0.02s ===========================================================================
3、步骤
如果你想对每个测试调用进行非常详细的逐步说明,可以通过@allure.step装饰器来实现(固件同样支持)。
该装饰器会将方法或函数的调用与提供的参数一起添加到报表中,并且可以包含一条描述行,该行支持位置和关键字参数。
示例:
# test_sample.py
import pytest
import allure
@allure.step('两数相加:{0} + {y}')
def add(x, y):
r = x + y
print_res(r)
return r
@allure.step
def print_res(r):
print('计算结果:', r)
class TestLearning:
data = [
[3, 4, 7],
[-3, 4, 1],
[3, -4, -1],
[-3, -4, -7],
]
@pytest.mark.parametrize("data", data)
def test_add(self, data):
assert add(data[0], data[1]) == data[2]
报告:
4、标题
如果你想让测试标题更具可读性,可以使用@allure.title装饰器,该装饰器支持参数的占位符并支持动态替换。
示例:
# test_sample.py
import pytest
import allure
def add(x, y):
return x + y
class TestLearning:
data = [
[3, 4, 7],
[-3, 4, 1],
[3, -4, -1],
[-3, -4, -7],
]
@allure.title("测试用例-{data}")
@pytest.mark.parametrize("data", data)
def test_add(self, data):
assert add(data[0], data[1]) == data[2]
报告:
5、描述
如果你想添加测试的详细说明,可以通过添加测试方法描述信息,也可以使用装饰器@allure.description
和@allure.description_html
。
示例:
# test_sample.py
import allure
# 被测功能
def add(x, y):
return x + y
# 测试类
class TestLearning:
@allure.description('测试正数相加')
def test_first(self):
assert add(3, 4) == 7
@allure.description_html('<h1> 测试负数相加 </h1>')
def test_second(self):
"""你也可以在这里添加用例的描述信息,但是会被allure装饰器覆盖"""
assert add(-3, -4) == -7
报告:
6、附件
如果你想在报告中显示不同类型的附件,可以通过以下两种方式来实现:allure.attach
(body, name, attachment_type, extension)allure.attach.file
(source, name, attachment_type, extension)
示例:
import allure
def test_multiple_attachments():
allure.attach.file(r'C:\Users\Public\Pictures\Sample Pictures\Koala.jpg', attachment_type=allure.attachment_type.JPG)
allure.attach('<head></head><body> 测试页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)
报告:
7、链接
如果你想关联缺陷追踪系统或测试管理系统,你可以使用装饰器@allure.link
、@allure.issue
、@allure.testcase
。
示例:
import allure
@allure.link('http://www.baidu.com')
def test_with_named_link():
pass
@allure.issue('101', '缺陷问题描述')
def test_with_issue_link():
pass
@allure.testcase('http://this.testcase.com', '测试用例标题')
def test_with_testcase_link():
pass
报告:
注意:
@allure.issue 将提供带有小错误图标的链接,该描述符将测试用例ID作为输入参数,以将其与提供的问题链接类型的链接模板一起使用。
链接模板在 --allure-link-patternPytest 的配置选项中指定。链接模板和类型必须使用冒号指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}
官方文档地址:https://docs.qameta.io/allure/
来源:https://www.cnblogs.com/chenyablog/p/15145370.html


猜你喜欢
- Python用Pillow(PIL)进行简单的图像操作方法颜色与RGBA值计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透
- 需求需要生成一个宣传的图片分享到朋友圈,这个宣传图片包含二维码,包含不同的背景图片和不同的文字。对于这种图片生成,我们考虑过使用服务端生成,
- 如下所示:arrs=[2,15,48,4,5,6,7,6,4,1,2,3,6,6,7,4,6,8]f=open('test.txt&
- 一、摘要Python使用被称为异常 的特殊对象来管理程序执行期间发生的错误。每当发生让Python不知所措的错误时,它都会创建一个异常对象。
- 写在之前我们都知道 Python 中内置了许多标准的数据结构,比如列表,元组,字典等。与此同时标准库还提供了一些额外的数据结构,我们可以基于
- 这篇文章主要介绍了python 采用paramiko 远程执行命令及报错解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- django-mdeditorGithub地址:https://github.com/pylixm/django-mdeditor 欢迎试用
- 阅读上一篇:打造设计你自己的字体 Ⅱ永远都在寻觅字体设计的灵感。夏天过后,我买了一套便宜的书法钢笔,说服自己,它会让我的鸡爬字产生脱胎换骨的
- 子线程里是不能更新UI界面的,在移动端方面。Android的UI访问是没有加锁的,多个线程可以同时访问更新操作同一个UI控件。也就是说访问U
- 树状图树状图是显示对象、组或变量之间的层次关系的图表。树状图由在节点或簇处连接的分支组成,它们代表具有相似特征的观察组。分支的高度或节点之间
- try-except作用:处理异常情况用法:try:后面写正常运行的代码,except + 异常情况:后面写对异常情况的处理示例:try:
- 当我想要完美的使用:nth-child或者:nth-of-type的时候有点儿头晕。你越理解它们,就能写出越好的CSS规则!在这些简单的”秘
- 传统的网页BBS大多是采用CGI模式实现的,它的实现要求编程者既要掌握编程语言如Perl或C等,又要了解关于CGI模式的各项技术内容,因此要
- 一、前言听说python很流行,因为有很多模块资源,而且导入模块,操作和理解起来很简单。所以在这里记录一下学习python的过程,我相信最重
- 我们在做表单的时候经常会使用到这样的结构:<fieldset> <lege
- 有如下实现方法: 在Firefox, Google Chrome, Safari, Opera中:可以用 window.getSelecti
- c shell perl php下的日期时间转换: 秒数与人类可读日期 scalar localtime 与 seconds since `
- 先利用pip安装pymssql库pip install pymssql具体连接、测试代码:# server默认为127.0.0.1,如果打开
- 1 from multiprocessing import Pool,Queue。其中Queue在Pool中不起作用,具体原因未明。解决方案
- 案例一:运行下面的代码结果是什么?class Person: def run(self): &nbs