一、简介
简而言之 Allure是一个灵活的轻量级多语言测试报告工具,它可以通过简洁的Web报告形式显示了已测试内容详情,并允许使用者自由定制想要提取信息。
详见参考指南文档:https://docs.qameta.io/allure
说明:本文为学习笔记,如错误、可优化等内容,欢迎交流与指正。
二、安装
0、 前提:安装并配置好jdk环境
1、 Allure下载地址:https://github.com/allure-framework/allure2/releases(在Windows上安装下载zip格式)
2、将下载的压缩包解压到安装位置
3、 配置Path变量:查看高级系统设置→环境变量→系统变量→Path→新建1条: 你的路径\allure-2.19.0\bin
4、CMD中验证方式:
allure --version
显示当前安装的allure版本号;allure
显示用法信息;where allure
显示allure程序所在路径。
5、在python中或pycharm安装allure-pytest插件: pip install allure-pytest
三、使用(以结合Pytest使用为例)
1、设置.json报告文件存放位置
在pytest.ini文件中添加一行 addopts = -vs --alluredir ../temp
设置allure报告存放位置:
-vs
pytest参数,视需要添加;--alluredir
设置.json报告文件存储路径;../temp
将报告文件存在上级目录下的temp中。
此时执行完 pytest.main()
,allure 生产会后缀为.json的报告文件。
2、生成HTML格式报告文件
在pycharm中,可通过执行
os.system("allure generate ../temp -o ../reports --clean")
生产HTML格式报告文件
-o ../reports
是设置HTML报告输出目录--clean
是在生产新报告前清除输出目录(简写-c
也可以)
3、查看精美的测试报告
方式一:如果已经生成了HTML报告,直接点击响应文件即可查看
方式二:也可以在终端输入 allure serve ./temp
命令直接查看,其中 ./temp
为.json报告文件存放路径。
4、常用功能
通过上面方式直接生成的报告虽然已经够精美,但内容呈现仍很简陋,通过添加allure的常用功能后,便能够在报告中呈现出更丰富的信息。
@allure.feature('测试模块名称')
,添加在测试类(class)前@allure.story('测试用例名称')
,添加在测试方法(def)前with allure.step('测试步骤名称')
,添加在需要记录的测试步骤前,视需要添加allure.attach(body, name=None, attachment_type=None, extension=None)
- 添加在需要以附件形式记录信息的位置
- body:写入附加中的正文内容
- name:附件名称
- attachment_type:附加类型,例如:
allure.attachment_type.TEXT
,具体支持格式可查看allure.attachment_type
模块文件 - extension:附加拓展名,实际在allure.attachment_type模块中已具有
allure.attach.file(source, name=None, attachment_type=None, extension=None)
- 添加在需要上传至allure报告中附件,如在运行失败用例的截图
- source:文件路径
- environment.properties:环境属性配置文件,可在生成HTML报告前创建此文件用于显示测试环境等信息,例如:
- projectName=testAllure
- systemVersion=win11
- pythonVersion=3.9.10
- testor=dwh
"""
测试报告
"""
import allure
import pytest
@allure.feature('测试allure模块装饰器@allure.feature')
class TestAllure():
"""测试allure类"""
@allure.story('测试allure用例装饰器@allure.story')
def test_001(self):
"""test_001"""
with allure.step('测试步骤1'):
print("test_001")
with allure.step('测试步骤2'):
print("test_002")
assert 1 == 1
@allure.story('测试allure.attach()')
def test_002(self):
"""test_002"""
with allure.step('测试步骤1'):
print("test_002")
allure.attach("打印了test_002", name="allure.attach()测试", attachment_type=allure.attachment_type.TEXT)
assert 1 == 1
@allure.story('测试allure.attach.file()')
def test_003(self):
"""test_003"""
allure.attach.file("D:\Mydata\HNWY\dwhtest/ttt.jpg", name="allure.attach.file()测试", attachment_type=allure.attachment_type.JPG)
assert 1 == 1
@allure.story('断言失败用例')
def test_004(self):
"""test_004"""
print("test_004")
assert 1 == 0
总览界面内容更加丰富,呈现了环境、特性场景等信息。
功能界面呈现了用例标题、测试步骤、附加等内容
说明:本文为学习笔记,如错误、可优化等内容,欢迎交流与指正。
Original: https://blog.csdn.net/Wenhua_1/article/details/127189033
Author: 楼宇野人
Title: Allure在Pytest自动化测试框架中的基本应用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/772889/
转载文章受原作者版权保护。转载请注明原作者出处!