本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28364

Allure2 报告中添加附件

TEXT = ("text/plain", "txt")
CSV = ("text/csv", "csv")
TSV = ("text/tab-separated-values", "tsv")
URI_LIST = ("text/uri-list", "uri")

HTML = ("text/html", "html")
XML = ("application/xml", "xml")
JSON = ("application/json", "json")
YAML = ("application/yaml", "yaml")
PCAP = ("application/vnd.tcpdump.pcap", "pcap")

PNG = ("image/png", "png")
JPG = ("image/jpg", "jpg")
SVG = ("image/svg-xml", "svg")
GIF = ("image/gif", "gif")
BMP = ("image/bmp", "bmp")
TIFF = ("image/tiff", "tiff")

MP4 = ("video/mp4", "mp4")
OGG = ("video/ogg", "ogg")
WEBM = ("video/webm", "webm")

PDF = ("application/pdf", "pdf")

Allure2 报告中添加附件(图片)应用场景

  • 应用场景:在做 UI 自动化测试时,可以将页面截图,或者出错的页面进行截图,将截图添加到测试报告中展示,辅助定位问题。
  • 解决方案:
  • Python:使用 allure.attach 或者 allure.attach.file() 添加图片。
  • Java:直接通过注解或调用方法添加。

Allure2 报告中添加附件(图片)- Python

  • 语法:allure.attach.file(source, name, attachment_type, extension),参数解释:
  • source:文件路径,相当于传一个文件。
  • name:附件名字。
  • attachment_type:附件类型,是 allure.attachment_type 其中的一种(支持 PNG、JPG、BMP、GIF 等)。
  • extension:附件的扩展名。
"""
@Author: 霍格沃兹测试开发学社-西西
@Desc: 更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860
"""
import allure

class TestWithAttach:
    def test_pic(self):
        allure.attach.file("pic.png",
                           name="图片",
                           attachment_type=allure.attachment_type.PNG,
                           extension="png")

Allure2 报告中添加附件(图片)- Python

  • 语法:allure.attach(body, name=None, attachment_type=None, extension=None):,参数解释:
  • body:要写入附件的内容
  • name:附件名字。
  • attachment_type:附件类型,是 allure.attachment_type 其中的一种(支持 PNG、JPG、BMP、GIF 等)。
  • extension:附件的扩展名。
"""
@Author: 霍格沃兹测试开发学社-西西
@Desc: 更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860
"""
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)

Allure2 报告中添加附件(图片)- Java

Allure 支持两种方法: - 注解方式添加。

  • 调用方法添加。

注解方式 - Java

  • 注解方式直接传入图片。
    @Attachment(value = “图片名”, type = “image/png”, fileExtension = “后缀”)

调用方法添加 - Java

  • 使用Allure方法传入图片。
    Allure.addAttachment(“图片名”, “image/png”,
    图片路径, “后缀”);

裂图的原因以及解决办法

  1. 图片上传过程中出现了网络中断或者传输过程中出现了错误。
  • 解决方案:重新上传图片。
  1. Allure 报告中的图片大小超过了 Allure 的限制。
  • 解决方案:调整图片大小。
  1. 图片本身存在问题。
  • 解决方案:检查图片格式和文件本身
package com.junit5.allure2report_addattachment_l3.addpicture;

import io.qameta.allure.Allure;
import io.qameta.allure.Attachment;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * 附件为图片的添加
 * 应用场景:
 * 1、UI\APP自动化
 * 2、测试用例失败的时候
 * 3、预期结果
 */
@DisplayName("allure图片添加")
public class PngTest {
    @Test
    @DisplayName("allure图片注解的添加")
    public void testAddPngWithAttachmentAn() throws IOException {
        byte[] contents = Files.newInputStream(Paths.get("allure2.png")).readAllBytes();
        attachPngFile(contents,"allure图");
    }

    @Attachment(value = "{pngName}", type = "image/png", fileExtension = "png")
    public byte[] attachPngFile(byte[] contents, String pngName) {
        System.out.println("png:"+pngName);
        return contents;
    }


    @Test
    @DisplayName("allure图片方法的添加")
    public void testAddPngWithAttachmentMethode01() throws IOException {
        Allure.addAttachment("截图","image/png",
                Files.newInputStream(Paths.get("allure202.png")),"png");
    }

}

软件测试学习笔记丨Allure2报告中添加附件-图片_java

软件测试学习笔记丨Allure2报告中添加附件-图片_java_02