Java Allure 报告中增加Environment属性信息

2023-01-14 11:17:29 浏览数 (1)

通过allureEnvironmentWriter即可给allure报告增加environment显示信息。此段代码会默认在target/allure-results文件夹生成一个environment.xml文件,但是想要在报告中显示,还需要将这个environment.xml复制到外层的allure-results文件夹中。

“所以如果你的environment显示信息是固定的,你可以直接在allure-results文件夹中建一个environment.xml文件,内容填写你想要在报告中展示的环境信息。”

代码如下:

代码语言:javascript复制
package com.demo.testng;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.testng.annotations.*;


import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


import org.apache.commons.io.FileUtils;
import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;


public class TestNGDemo1 {

    @BeforeSuite
    void setAllureEnvironment() throws IOException {
        allureEnvironmentWriter(
                ImmutableMap.<String, String>builder()
                        .put("Browser", "Chrome")
                        .put("Author", "孟船长")
                        .put("公众号", "自动化测试实战")
                        .build());


        File envTarget = new File("allure-results/environment.xml");
        File envSrc = new File("target/allure-results/environment.xml");
        FileUtils.copyFile(envSrc, envTarget);
    }


    @BeforeClass
    public void openBrowser(){
        System.out.println("Open browser of @BeforeMethod");
    }


    @AfterClass
    public void closeBrowser(){
        System.out.println("Close browser of @AfterMehtod");
    }


    @Test(groups = {"groups01"})
    public void test01(){
        System.out.println("This is test01");
    }


    @Test(groups = {"groups01", "groups02"})
    public void test02(){
        System.out.println("This is test02");
    }
}

依赖

代码语言:javascript复制
<dependency>
	<groupId>com.github.automatedowl</groupId>
	<artifactId>allure-environment-writer</artifactId>
	<version>1.0.0</version>
</dependency>

在终端Terminal中先执行用例mvn clean test

再查看报告allure serve allure-results

xml

0 人点赞