Allure: Environment file in target folder gets del

2019-05-11 17:13发布

问题:

The instructions say to add the environment.xml to the Allure results directory (https://github.com/allure-framework/allure-core/wiki/Environment) but this folder gets deleted on mvn clean so the files gets deleted with it. Is there a way to generate this file on every build?

Thanks.

回答1:

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>


回答2:

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> 


回答3:

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);
    }
}