本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/32336
安装与下载
需要下载本地文件,并且添加到环境变量里
windows:下载,解压,并配置环境变量
mac:brew install allure
环境变量:将bin目录纳入path路径中
python安装第三方依赖
win:pip install allure-pytest
mac:pip3 install allure-pytest
allure-pytest
依赖会自动安装Allure-pytest和Allure-python-commons
包,以生成与Allure 2
兼容的报告数据
基本用法
命令行执行,在进行pytest测试时,生成allure数据:
pytest --allure=./allure-results
命令行执行,将生成的allure数据解析出来,展示在浏览器中:
allure serve ./allure-results
Allure 注解
使用方法 | 参数值 | 参数说明 |
@allure.epic() | epic描述 | 敏捷里面的概念,定义史诗,往下是feature |
@allure.feature() | 模块名称 | 功能点的描述,往下是story |
@allure.story() | 用户故事 | 用户固戍,往下是title |
@allure.title | 用例的标题 | 重命名HTML报告名称 |
@allure.testcase() | 测试用例的链接地址 | 对应功能测试用例系统里的case |
@allure.issue | 缺陷 | 对应缺陷管理系统里面的链接 |
@allure.description() | 用例描述 | 测试用例的描述 |
@allure.step() | 操作步骤 | 测试用例的步骤 |
@allure.severity() | 用例等级 | blocker,critical,normal,minor,trivial |
@allure.link() | 链接 | 定义一个链接在测试报告展示 |
@allure.attachment() | 附件 | 报告添加附件 |
import allure
import pytest
@allure.feature('test_success')
def test_success():
"""this test succeeds"""
assert True
@allure.feature('test_failure')
def test_failure():
"""this test fails"""
assert False
@allure.feature('test_skip')
def test_skip():
"""this test is skipped"""
pytest.skip('for a reason!')
@allure.feature('test_broken')
def test_broken():
raise Exception('oops')
if __name__ == '__main__':
# pytest.main(["-s","allure-test.py"])
'''
-q: 安静模式, 不输出环境信息
-v: 丰富信息模式, 输出更详细的用例执行信息
-s: 显示程序中的print/logging输出
'''
pytest.main(['-s', '-q','test_allure02.py','--clean-alluredir','--alluredir=allure-results'])
os.system(r"allure generate -c -o allure-report")
在报告中添加图片附件,文件附件
安装第三方依赖
pip install pytest-allure-adaptor
@allure.attach的用法
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:
文件路径,相当于传一个文件name:
附件名字attachment_type:
附件类型,是allure.attachment_type
其中的一种extension:
附件的拓展名
使用范例(添加文本文件)
# file_name: test_allure_attachments.py
import pytest
import allure
@pytest.fixture()
def attach_for_text():
allure.attach(body="这是一段文本,setUp", name="test文本01", attachment_type=allure.attachment_type.TEXT)
yield
allure.attach(body="这是一段文本,teardown", name="test文本02", attachment_type=allure.attachment_type.TEXT)
def test_attachment_text(attach_for_text):
pass
if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])
使用范例(添加图片和html)
# file_name: test_allure_attachments.py
import pytest
import allure
def test_mutiple_attachments():
allure.attach.file("./pytest_study/image/pikaqiu.jpg", attachment_type=allure.attachment_type.JPG)
allure.attach("<html><body><font color='red'>这是一段html</font></body></html>",
attachment_type=allure.attachment_type.HTML)
if __name__ == '__main__':
pytest.main(['-s', 'test_allure_attachments.py'])