Easily manage Properties Files in Java with Owner
January 3, 2021100 Best tips about Java Testing tools
February 7, 2021Introduction
Instead of using some approaches that can add an extra abstraction layer to deliver a pretty report, we can use one with less code showing similar results using Allure Reports.
The report can show all the test information and metrics. Sometimes we would like to add extra/custom information to remember the configuration set, the browser used, users, endpoints, etc…
You will learn how to add any additional information to the Allure Report. You will no miss any important information.
The problem
Allure Reports has an Environment section where you can place environment information using a .properties
or .xml
file into the allure-results
directory. You can see the official explanation here.
The problem is you need to add or change it manually to see the information in the report.
The Solution
The allure-environment-writer Java library allows the automatic environment file generation, adding it into the allure-results
folder.
Import the library
Add the dependency in your preferred build tool. This is the example using Maven:
<dependency>
<groupId>com.github.automatedowl</groupId>
<artifactId>allure-environment-writer</artifactId>
<version>1.0.0</version>
</dependency>
Adding the environment data
A good practice is to use the environment writer as the first pre-condition in your test, like @BeforeSuite
for TestNG or @BeforeAll
for JUnit 5.
We will use the allureEnvironmentWriter
static method from the AllureEnvironmentWriter
class. This method receives an ImmutableMap
from the Guava library.
We must create a key-value immutable map and at its values to the reports Environment section.
import com.google.common.collect.ImmutableMap;
import static com.github.automatedowl.tools.AllureEnvironmentWriter.allureEnvironmentWriter;
public class ExampleTest {
@BeforeSuite
void setAllureEnvironment() {
allureEnvironmentWriter(
ImmutableMap.<String, String>builder()
.put("Browser", "Chrome")
.put("Browser.Version", "87.0.4280.88")
.put("URL", "https://eliasnogueira.com")
.build());
}
@Test
void myTest() {
// test goes here
}
}
As you can see we are adding the browser type, browser version, and URL information to the Allure Report @BeforeSuite
to set this data.
Run the test
Now you can run the test and generate the report. The report will look like the image below (click on it to expand):
Usage tip
Instead of writing the environment information into the class and modifying it in every execution, we can use the information we have stored in some configuration like a properties file.
Real example
The selenium-java-lean-test-architecture project has the following architecture decision to add environment information to the Allure Report.
- This project has three different configuration files
- general.properties: general configuration
- grid.properties: grid specific configuration
- local.properties: local test specific configuration
- AllureManager class: this class has the environment data to set into the report and the screenshots. You can see that the data passed to the environment is provided by the Configuration class. This class loads all the property values. We can attach all the configurations applied to the test.
All extra information will be added in the Environment section after a test run.
11 Comments
It didn’t work for me neither with @BeforeSuite nor with @BeforeAll.
Hi Dmytro,
Any exception or code example?
Best!
how i add web service request and response file
Hey Praveenraj,
It’s not possible to add files as environment information.
If you would like to see all requests and responses I would suggest you use any logging strategy, saving it in a file.
Oi Elias, será que poderia me auxiliar com uma questão?
Tenho umas aulas que ao clicar abre uma guia com o PDF, é referente a uns testes que precisa gerar o PDF, porém não sei como apresentar eles no relatório allure no Test body, sabe como me ajudar com isso, qual anotação, que jeito eu faria isso?
Olá Neia,
Eu não entendi muito bem. Tu quer mostrar o conteudo do PDF no relatório do Allure ou somente mostrar que o PDF foi baixado?
You should use Allure.addAttachment to the report, as the example below:
Allure.addAttachment(“Response body was: “, response.getResponse().getContentAsString());
Allure.addAttachment(“Status code was: “, String.valueOf(response.getResponse().getStatus()));
when I add this library, I cannot run test. It just opens browser and doesn’t doing anything
Is there any exception you can share?
How is this approach better than adding the environment.properties file? Do you have an example to compare both? Thanks for your post!
Hi Wendy,
The main difference, for me, is that you can add any information that you can get at any point during your test execution, making it dynamic.
Otherwise, it’s the same 🙂