Allure2 报告中添加附件(html)应用场景
- 应用场景:可以定制测试报告页面效果,可以将 HTML 类型的附件显示在报告页面上。
- 解决方案:
- Python:使用
allure.attach()
添加 html 代码。 - Java:直接通过注解或调用方法添加
- Python:使用
Allure2 报告中添加附件(html)- Python
- 语法:
allure.attach(body, name, attachment_type, extension)
,参数解释:- body:要写入附件的内容(HTML 代码块)。
- name:附件名字。
- attachment_type:附件类型,是
allure.attachment_type
其中的一种。 - extension:附件的扩展名。
class TestWithAttach:
def test_html(self):
allure.attach('<head></head><body> a page </body>',
'附件是HTML类型',
allure.attachment_type.HTML)
def test_html_part(self):
allure.attach('''html代码块''',
'附件是HTML类型',
allure.attachment_type.HTML)
Allure2 报告中添加附件(html) - Java
Allure 支持两种方法: - 注解方式添加。
- 调用方法添加。
注解方式 - Java
- 直接传入html文件。
@Attachment(value = “html名”, type = “text/html”, fileExtension = “后缀”)
调用方法添加 - Java
- 使用Allure方法传入html。
Allure.addAttachment(“html名”, “text/html”,
图片路径, “后缀”);
实例
package com.junit5.allure2report_addattachment_l3.addhtml;
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.*;
import java.nio.file.Files;
import java.nio.file.Paths;
@DisplayName("Allure2测试报告添加html")
public class HtmlTest {
/**
* 注解:String类型
*/
@Test
@DisplayName("html注解:注解形式添加")
public void testAddHtmlForStringWithAn() throws IOException {
File file = new File("allure2.html");
// BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line=bufferedReader.readLine())!=null){
stringBuffer.append(new String(line.getBytes(), "GBK"));
stringBuffer.append(System.lineSeparator());
}
attachTextFileWithString(stringBuffer.toString(),"百度首页注解");
}
@Attachment(value = "htmlName",type = "text/html")
String attachTextFileWithString(String contents,String htmlName){
System.out.println("htmlName:"+htmlName);
return contents;
}
@Test
@DisplayName("Html:调用方法添加")
public void testAddHtmlForStreamWithMethod() throws IOException {
Allure.addAttachment("stream流添加html",
"text/html",
Files.newInputStream(Paths.get("allure2.html")),
"html");
}
}
/**
* 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")
*/