allure 报告中添加附件的几种类型
前言
allure报告中添加附件
附件的几种类型
类型 | 类型值 |
---|---|
文本 | allure.attachment_type.TEXT |
CSV | allure.attachment_type.CSV |
图片 | allure.attachment_type.JPG或PNG |
allure.attachment_type.PDF | |
html文件 | allure.attachment_type.HTML |
json文件 | allure.attachment_type.JSON |
xml文件 | allure.attachment_type.XML |
mp4 | allure.attachment_type.MP4 |
添加字符串附件
语法示例
allure.attach(body,name,attachment_type,extention)
参数说明:
- body 要添加的内容
- name 附件的文件名
- attachment_type 如text
- extention 附件保存的文件后缀(可不填)
添加请求和响应日志附件
import requests
import allure
import json
def test_allure():
url = 'http://httpbin.org/post'
body = {
"user": "test_xx",
"password": "123456"
}
allure.attach(json.dumps(body), 'request', allure.attachment_type.TEXT)
res = requests.post(url, json=body)
print(f"返回结果:{res.text}")
allure.attach(res.text, 'response', allure.attachment_type.TEXT)
allure报告中查看附件
添加图片附件
语法:
allure.attach.file(source, name, attachment_type, extension)
参数解释:
- source:文件路径,相当于传一个文件。
- name:附件名字。
- attachment_type:附件类型,是 allure.attachment_type 其中的一种。
- extension:附件的扩展名。
添加图片示例
import requests
import allure
import json
from pathlib import Path
# 作者-上海悠悠 微信/QQ交流:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/
def test_allure_img():
"""添加图片附件"""
print("----添加附件")
file_path = Path(__file__).parent.joinpath('abc.jpg')
allure.attach.file(file_path, '添加的图片', allure.attachment_type.JPG, extension="jpg")