Writing Environment Values to Allure Report in Java
Aiming to extend the Allure framework with a nice capability, I developed Java library which allows writing key/value pairs to environment.xml file on runtime, using allure-results directory. Currently, the library supports Allure report with either JUnit or TestNG. It would allow you to:
- Easily write key/value set to environment.xml file to allure-results directory in any stage of your test.
- Define the configurable path of ‘allure-results’ directory.
- Use ImmutableMap object to create an immutable set of values for the environment to contain.
- GitHub link: https://github.com/AutomatedOwl/allure-environment-writer
Example of common usage (default path is target/allure-results):
In the TestNG method below, we are making an ImmutableMap object containing hard-coded set of desired keys/values to appear in allure report under ‘environment’ section. We’re using the default path for allure-results folder. Behind the scenes, a factory method iterating over the map and writing each key/value pair to the XML file, in a designated tags.
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());
} @Test
void sanityOneTest() {
Assert.assertTrue(true);
} @Test
void sanityTwoTest() {
Assert.assertTrue(true);
}
}
Example of usage with customized allure-results path:
The TestNG method below is just the same as the example about, just with the usage of configurable allure-results path, passed as ‘optional’ parameter to the allureEnvironmentWriter method.
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);
}
}
Snapshot of received environment data in Allure report:
Maven dependencies
<dependency>
<groupId>com.github.automatedowl</groupId>
<artifactId>allure-environment-writer</artifactId><version>1.0.0</version>
</dependency>