通过allureEnvironmentWriter 即可给allure 报告增加environment显示信息。此段代码会默认在target/allure-results 文件夹生成一个environment.xml 文件,但是想要在报告中显示,还需要将这个environment.xml 复制到外层的allure-results 文件夹中。 “所以如果你的environment 显示信息是固定的,你可以直接在allure-results 文件夹中建一个environment.xml 文件,内容填写你想要在报告中展示的环境信息。 ” 代码如下: 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"); } }
依赖 <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
|