Skip to content

Allure2 报告中添加附件 - 图片


简介

Allure2 支持多种格式的附件,可灵活添加到测试报告中,以使得报告更加清晰、问题定位更加准确。这些附件格式包括:

  • 文本文件:TXT、CSV、TSV、URI 列表等
  • 标记语言文件:HTML、XML、JSON、YAML
  • 图像文件:PNG、JPG、SVG、GIF、BMP、TIFF
  • 视频文件:MP4、OGG、WEBM
  • 其他格式:PDF

Allure2 报告中添加图片附件

在做 UI 自动化测试时,可以将页面截图,或者出错的页面进行截图,将截图添加到测试报告中展示。


Python 版本

方法一

  • 语法:allure.attach.file(source, name, attachment_type, extension)
  • 参数解释:
    • source:文件路径,相当于传一个文件。
    • name:附件名字。
    • attachment_type:附件类型,是 allure.attachment_type 其中的一种(支持 PNG、JPG、BMP、GIF 等)。
    • extension:附件的扩展名。
import allure

class TestWithAttach:
    def test_pic(self):
        allure.attach.file("pic.png"#这里需改成自己想要的图片的路径,
                           name="图片",
                           attachment_type=allure.attachment_type.PNG,
                           extension="png")

方法二

  • 语法:allure.attach(body, name=None, attachment_type=None, extension=None):
  • 参数解释:
    • body:要写入附件的内容
    • name:附件名字。
    • attachment_type:附件类型,是 allure.attachment_type 其中的一种(支持 PNG、JPG、BMP、GIF 等)。
    • extension:附件的扩展名。
import allure

class TestWithAttach:
    def test_pic2(self):
        with open("./img/logo.png",mode="rb") as f :
            file = f.read()
            allure.attach(file,"页面截图",allure.attachment_type.PNG)

Java 版本

方法一:注解方式

使用 @Attachment 注解直接传入图片。

@Attachment(value = "图片名", type = "image/png", fileExtension = "后缀")

方法二:调用方法添加

使用 Allure.addAttachment 方法传入图片。

Allure.addAttachment("图片名", "image/png",
      图片路径, "后缀");

产生裂图的原因以及解决办法

  1. 图片上传过程中出现了网络中断或者传输过程中出现了错误。

    • 解决方案:重新上传图片。
  2. Allure 报告中的图片大小超过了 Allure 的限制。

    • 解决方案:调整图片大小。
  3. 图片本身存在问题。

    • 解决方案:检查图片格式和文件本身。

总结

  • Allure 报告中支持添加的附件格式
  • 在 Allure 报告中添加图片附件
  • Allure 报告中裂图的原因以及解决办法