本文转自测试人社区,原文链接: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 注解
import allureimport 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)
参数说明:
2、 allure.attach.file(source,name,attachment_type,extension)
参数说明:
使用范例(添加文本文件)
# file_name: test_allure_attachments.py
import pytestimport 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 pytestimport 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'])
复制代码
评论