allure.attach
allure.attach用于在测试报告中添加附件,补充测试结果。附件格式可以是txt、jpg等,附件内容通常是测试数据、截图等。
allure.attach提供了两种方法:allure.attach(),allure.attach.file()
allure.attach()
作用:在测试报告中生成指定内容、名称、类型的附件
语法:allure.attach(body, name=None, attachment_type=None, extension=None)
参数说明:
body,需要显示的内容,也可以理解为写入附件的内容name,附件名称attachment_type,附件类型,如csv、jpg、html 等,由allure.attachment_type提供extension:附件扩展名,不常用
allure.attach.file()
作用:向测试用例中上传附件
语法:allure.attach.file(source, name=None, attachment_type=None, extension=None)
参数说明:source为文件路径,其他参数与allure.attach()参数一致。
在UI自动化测试中,会经常用到这个方法来上传用例执行的截图。
示例
test_login.py:
import allure
import pytest
import requests
import json
data = [("lilei", "123456"), ("hanmeimei", "888888"), ("xiaoming", "111111")]
ids = ["username:{}-password:{}".format(username, password) for username, password in data]
@allure.epic("xx在线购物平台接口测试")
@allure.feature("登录模块")
class TestLogin:
@allure.story("用户登录")
@allure.title("登录")
@pytest.mark.parametrize("username, password", data, ids=ids)
def test_login(self, username, password):
headers = {"Content-Type": "application/json;charset=utf8"}
url = "http://127.0.0.1:5000/login"
_data = {
"username": username,
"password": password
}
allure.attach(
body="用户名-{},密码-{}".format(username, password),
name="登录参数",
attachment_type=allure.attachment_type.TEXT
)
res = requests.post(url=url, headers=headers, json=_data).text
res = json.loads(res)
assert res['code'] == 1000
@allure.story("用户退出登录")
@allure.title("退出登录")
def test_logout(self):
'''这条测试用例仅仅只是为了举例说明allure.attach.file的使用'''
print("退出登录,并截图")
# 截图路径
testcase_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
source_path = testcase_path + "/screenshot/logout.jpg"
allure.attach.file(
source=source_path,
name="退出登录后截图",
attachment_type=allure.attachment_type.JPG
)
assert True
上述代码中使用了@pytest.mark.parametrize()

本文介绍了如何在Python的UI自动化测试中使用Allure.attach()和allure.attach.file()方法添加测试报告附件,包括文本和文件,并展示了如何配合pytest的参数化、fixture和测试步骤功能。同时,还讲解了如何配置环境信息和自定义缺陷分类。
最低0.47元/天 解锁文章
1008

被折叠的 条评论
为什么被折叠?



