Python UI自动化-Allure-report添加截图后无法显示问题,已解决。

文章介绍了在使用allure.attach()函数添加截图附件时遇到的问题,即截图在报告中无法显示。问题在于函数需要二进制内容而不是文件路径。解决方案包括以二进制方式读取文件内容再附加,或直接使用allure.attach.file()方法指定文件路径。两种修改后的写法使得截图在allure报告中能正常显示。
摘要由CSDN通过智能技术生成

allure.attach()函数

allure.attach()函数可以在报告中添加附件:截图、文本、html等,使用方法如下:

方法一

allure.attach(body, name, attachment_type, extension)
  • body:要写入附件的内容; name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

方法二

allure.attach.file(source, name, attachment_type, extension)
  • source:文件路径,相当于传一个文件;
  • name:附件名字;
  • attachment_type:附件类型,是allure.attachment_type其中的一种;
  • extension:附件的扩展名;

遇到的问题

此处我使用的是方法一,截图在文件夹内可见,在allure报告内不可见。 一开始以为是加密软件的问题,后来查到是因为allure.attach()没有读取文件的过程,因此需要先以二进制的方式去读截图,然后放入allure报告内。或者是直接使用方法二来实现。

原写法

allure.attach(body=f"images/login/登录{case['caseid']}.png", name=f"登录{case['caseid']}.png",attachment_type=allure.attachment_type.PNG)

修改为

with open(f"images/login/登录{case['caseid']}.png", "rb") as f:
    content = f.read()
    allure.attach(body=content, name=f"登录{case['caseid']}.png", attachment_type=allure.attachment_type.PNG)

或者直接修改为方法二

allure.attach.file(f"images/login/登录{case['caseid']}.png", name=f"登录{case['caseid']}.png",attachment_type=allure.attachment_type.PNG)

图片可见。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值