Allure2报告添加用例优先级
- 应用场景:用例执行时,希望按照严重级别执行测试用例。
- 解决:可以为每个用例添加一个等级的装饰器,用法:
@allure.severity
。
** Allure 对严重级别的定义分为 5 个级别:*
-
Blocker级别:中断缺陷(客户端程序无响应,无法执行下一步操作)。
-
Critical级别:临界缺陷( 功能点缺失)。
-
Normal级别:普通缺陷(数值计算错误)。
-
Minor级别:次要缺陷(界面错误与UI需求不符)。
-
Trivial级别:轻微缺陷(必输项无提示,或者提示不规范)。
-
使用装饰器添加用例方法/类的级别。
-
类上添加的级别,对类中没有添加级别的方法生效。
-
运行时添加命令行参数
--allure-severities
:pytest --alluredir ./results --clean-alluredir --allure-severities normal,blocker
Allure2 添加用例标签应用场景
- Allure 报告支持的一些常见 Pytest 特性包括 xfail、skipif、fixture 等。
- 用法:使用装饰器
@pytest.xfail()
、@pytest.skipif()
import pytest
# 当用例通过时标注为 xfail
@pytest.mark.xfail(condition=lambda: True, reason='这是一个预期失败的用例')
def test_xfail_expected_failure():
"""this test is an xfail that will be marked as expected failure"""
assert False
# 当用例通过时标注为 xpass
@pytest.mark.xfail
def test_xfail_unexpected_pass():
"""this test is an xfail that will be marked as unexpected success"""
assert True
# 跳过用例
@pytest.mark.skipif('2 + 2 != 5', reason='当条件触发时这个用例被跳过 @pytest.mark.skipif')
def test_skip_by_triggered_condition():
pass
- fixture 和 finalizer 是分别在测试开始之前和测试结束之后由 Pytest 调用的实用程序函数。Allure 跟踪每个 fixture 的调用,并详细显示调用了哪些方法以及哪些参数,从而保持了调用的正确顺序。
import pytest
@pytest.fixture()
def func(request):
print("这是一个fixture方法")
# 定义一个终结器,teardown动作放在终结器中
def over():
print("session级别终结器")
request.addfinalizer(over)
class TestClass(object):
def test_with_scoped_finalizers(self,func):
print("测试用例")
Allure2报告中支持记录失败重试功能
- 重试功能可以使用 pytest 相关的插件,例如
pytest-rerunfailures
。 - 重试的结果信息,会展示在详情页面的”Retries” 选项卡中。
import pytest
@pytest.mark.flaky(reruns=2, reruns_delay=2)
def test_rerun2():
assert False
Allure2 报告中添加附件-图片
- 应用场景:在做 UI 自动化测试时,可以将页面截图,或者出错的页面进行截图,将截图添加到测试报告中展示,辅助定位问题。
- 解决方案:
- Python:使用
allure.attach
或者allure.attach.file()
添加图片。
- Python:使用
- 语法:
allure.attach.file(source, name, attachment_type, extension)
,参数解释:- source:文件路径,相当于传一个文件。
- name:附件名字。
- attachment_type:附件类型,是
allure.attachment_type
其中的一种(支持 PNG、JPG、BMP、GIF 等)。 - extension:附件的扩展名。
import allure
class TestWithAttach:
def test_pic(self):
allure.attach.file("pic.png",
name="图片",
attachment_type=allure.attachment_type.PNG,
extension="png")
语法:allure.attach(body, name=None, attachment_type=None, extension=None)
import allure
class TestWithAttach:
def test_pic2(self):
with open("./img/logo.png",mode="rb") as f :
file = f.read()
allure.attach(file,"页面截图",allure.attachment_type.PNG)
Allure2报告中添加附件-日志
- 应用场景:报告中添加详细的日志信息,有助于分析定位问题。
- 解决方案:
- Python:使用 python 自带的 logging 模块生成日志,日志会自动添加到测试报告中
- 日志配置,在测试报告中使用
logger
对象生成对应级别的日志。
# 创建一个日志模块: log_util.py
import logging
import os
from logging.handlers import RotatingFileHandler
# 绑定绑定句柄到logger对象
logger = logging.getLogger(__name__)
# 获取当前工具文件所在的路径
root_path = os.path.dirname(os.path.abspath(__file__))
# 拼接当前要输出日志的路径
log_dir_path = os.sep.join([root_path, f'/logs'])
if not os.path.isdir(log_dir_path):
os.mkdir(log_dir_path)
# 创建日志记录器,指明日志保存路径,每个日志的大小,保存日志的上限
file_log_handler = RotatingFileHandler(os.sep.join([log_dir_path, 'log.log']), maxBytes=1024 * 1024, backupCount=10 , encoding="utf-8")
# 设置日志的格式
date_string = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(
'[%(asctime)s] [%(levelname)s] [%(filename)s]/[line: %(lineno)d]/[%(funcName)s] %(message)s ', date_string)
# 日志输出到控制台的句柄
stream_handler = logging.StreamHandler()
# 将日志记录器指定日志的格式
file_log_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)
# 为全局的日志工具对象添加日志记录器
# 绑定绑定句柄到logger对象
logger.addHandler(stream_handler)
logger.addHandler(file_log_handler)
# 设置日志输出级别
logger.setLevel(level=logging.INFO)
Allure2 报告中添加附件-html
- 应用场景:可以定制测试报告页面效果,可以将 HTML 类型的附件显示在报告页面上。
- 解决方案:
- Python:使用
allure.attach()
添加 html 代码。
- Python:使用
- 语法:
allure.attach(body, name, attachment_type, extension)
class TestWithAttach:
def test_html(self):
allure.attach('<head></head><body> a page </body>',
'附件是HTML类型',
allure.attachment_type.HTML)
def test_html_part(self):
allure.attach('''html代码块''',
'附件是HTML类型',
allure.attachment_type.HTML)
Allure2 报告定制
页面 Logo
- 修改
allure.yml
文件,添加 logo 插件custom-logo-plugin
(在 allure 安装路径下,可以通过where allure
或者which allure
查看 allure 安装路径)。 - 编辑 styles.css 文件,配置 logo 图片。
/* 打开 styles.css 文件,
目录在:/xxx/allure-2.13.2/plugins/custom-logo-plugin/static/styles.css,
将内容修改为:*/
.side-nav__brand {
background: url("logo.png") no-repeat left center !important;
margin-left: 10px;
height: 40px;
background-size: contain !important;
}
页面标题
- 编辑 styles.css 文件,添加修改标题对应的代码。
/* 去掉图片后边 allure 文本 */
.side-nav__brand-text {
display: none;
}
/* 设置logo 后面的字体样式与字体大小 */
.side-nav__brand:after {
content: "霍格沃兹测试学社";
margin-left: 18px;
height: 20px;
font-family: Arial;
font-size: 13px;
}