Allure: Environment file in target folder gets deleted on maven clean. How do I generate it on every build?

元气小坏坏 提交于 2019-12-06 00:30:15

Just put in in your src/main/resources/ and copy to your results directory via maven resources plugin on mvn test or mvn site:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-allure-environment</id>
            <phase>pre-site</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/allure-results</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <includes>
                            <include>environment.xml</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

for me, the phase "pre-site" didn't work the correct phase is validate my resources are on the src\test\java\resoruces here is a working answer from my pom.xml file

<plugin>
   <artifactId>maven-resources-plugin</artifactId>
   <version>3.1.0</version>
   <executions>
      <execution>
         <id>copy-resources</id>
         <phase>validate</phase>
         <goals>
            <goal>copy-resources</goal>
         </goals>
         <configuration>
            <outputDirectory>${basedir}/allure-results</outputDirectory>
            <resources>
               <resource>
                  <directory>src/test/resources</directory>
                  <includes>
                     <include>environment.xml</include>
                  </includes>
               </resource>
            </resources>
         </configuration>
      </execution>
   </executions>
</plugin> 

Disclosure: I've created Java library which deals with this issue: https://github.com/AutomatedOwl/allure-environment-writer

It uses TransformerFactory to write the environment.xml to the allure-results path in any stage of the test. It also checks for the directory existence in case running from cleaned build.

Usage example:

import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;

public class SomeTests {

    @BeforeSuite
    void setAllureEnvironment() {
        allureEnvironmentWriter(
                ImmutableMap.<String, String>builder()
                        .put("Browser", "Chrome")
                        .put("Browser.Version", "70.0.3538.77")
                        .put("URL", "http://testjs.site88.net")
                        .build(), System.getProperty("user.dir")
                        + "/allure-results/");
    }

    @Test
    void someTest() {
        Assert.assertTrue(true);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!