Appearance
Allure JUnit 5
Generate beautiful HTML reports using Allure Report and your JUnit 5 tests.
Details
Check out the example projects at github.com/allure-examples to see Allure JUnit 5 (Jupiter) in action.
How to start
To integrate Allure into an existing JUnit 5 (Jupiter) project, you need to:
- Add Allure dependencies to your project.
- Set up AspectJ for
@Step
and@Attachment
annotations support. - Designate a location for Allure results storage.
Add Allure dependencies
xml
<!-- Define the version of Allure you want to use via the allure.version property -->
<properties>
<allure.version>2.25.0</allure.version>
</properties>
<!-- Add allure-bom to dependency management to ensure correct versions of all the dependencies are used -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-bom</artifactId>
<version>${allure.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Add necessary Allure dependencies to dependencies section -->
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
kts
// Define the version of Allure you want to use via the allureVersion property
val allureVersion = "2.25.0"
// ...
dependencies {
// Import allure-bom to ensure correct versions of all the dependencies are used
testImplementation(platform("io.qameta.allure:allure-bom:$allureVersion"))
// Add necessary Allure dependencies to dependencies section
testImplementation("io.qameta.allure:allure-junit5")
}
groovy
// Define the version of Allure you want to use via the allureVersion property
def allureVersion = "2.25.0"
dependencies {
// Import allure-bom to ensure correct versions of all the dependencies are used
testImplementation platform("io.qameta.allure:allure-bom:$allureVersion")
// Add necessary Allure dependencies to dependencies section
testImplementation "io.qameta.allure:allure-junit5"
}
Configure AspectJ
Allure leverages AspectJ for the functionality of @Step
and @Attachment
annotations. Additionally, some framework integrations (such as allure-assertj) rely on AspectJ integration to function correctly.
xml
<!-- Define the version of AspectJ -->
<properties>
<aspectj.version>1.9.21</aspectj.version>
</properties>
<!-- Add the following options to your maven-surefire-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
kts
// Define the version of AspectJ
val aspectJVersion = "1.9.21"
// Define configuration for AspectJ agent
val agent: Configuration by configurations.creating {
isCanBeConsumed = true
isCanBeResolved = true
}
dependencies {
// Add aspectjweaver dependency
agent("org.aspectj:aspectjweaver:${aspectJVersion}")
}
// Configure javaagent for test execution
tasks.test {
jvmArgs = listOf(
"-javaagent:${agent.singleFile}"
)
}
groovy
// Define the version of AspectJ
def aspectJVersion = '1.9.21'
// Define configuration for AspectJ agent
configurations {
agent {
canBeResolved = true
canBeConsumed = true
}
}
dependencies {
// Add aspectjweaver dependency
agent "org.aspectj:aspectjweaver:$aspectJVersion"
}
// Configure javaagent for test execution
test {
jvmArgs = [ "-javaagent:${configurations.agent.singleFile}" ]
}
Specifying Allure Results location
Allure, by default, saves test results in the project's root directory. However, it is recommended to store your test results in the build output directory.
To configure this, create an allure.properties
file and place it in the test resources directory of your project, which is typically located at src/test/resources
:
properties
allure.results.directory=target/allure-results
properties
allure.results.directory=build/allure-results
properties
allure.results.directory=build/allure-results
Run tests
Run your JUnit 5 tests the same way as you would run them usually. For example:
For Gradle:
bash
./gradlew test
bash
gradlew test
For Maven:
bash
./mvnw verify
bash
mvnw verify
After running the tests, Allure will gather the test execution data and store it in the allure-results
directory. You can then generate an HTML report from these results using Allure's reporting tools.
Generate a report
Finally, convert the test results into an HTML report. This can be done by one of two commands:
allure generate
processes the test results and saves an HTML report into theallure-report
directory. To view the report, use theallure open
command.Use this command if you need to save the report for future reference or for sharing it with colleagues.
allure serve
creates the same report asallure generate
but puts it into a temporary directory and starts a local web server configured to show this directory's contents. The command then automatically opens the main page of the report in a web browser.Use this command if you need to view the report for yourself and do not need to save it.
Writing tests
The Allure JUnit 5 adapter not only collects the data provided by JUnit 5's standard features, but also provides additional features for writing even better tests. This section lists the most notable ways to improve your tests, using both JUnit 5's and Allure JUnit 5's features.
With Allure JUnit 5, you can:
- provide description, links and other metadata,
- organize tests into hierarchies,
- divide the test into smaller, easier-to-read test steps,
- describe parameters used when running parametrized tests,
- make the test save screenshots and other files during execution,
- select which tests to run via a test plan file,
- provide arbitrary environment information for the whole test report.
Specify description, links and other metadata
There is a lot of metadata you can add to each test so that it would appear in the report. See the reference for more details.
For each of the metadata fields, there are two ways to assign it: via an annotation before a test method or via a method call inside a test method's body. The second way is called “dynamic”, because it allows you to construct strings and other values at runtime before passing to the methods. Note, however, that it is highly recommended to assign all metadata as early as possible. Otherwise, there is a risk of the test failing before having all metadata set, which is bad for the test report's readability.
java
import io.qameta.allure.Description;
import io.qameta.allure.Issue;
import io.qameta.allure.Link;
import io.qameta.allure.Owner;
import io.qameta.allure.Severity;
import io.qameta.allure.TmsLink;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static io.qameta.allure.SeverityLevel.*;
class TestMyWebsite {
@Test
@DisplayName("Test Authentication")
@Description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.")
@Severity(CRITICAL)
@Owner("John Doe")
@Link(name = "Website", url = "https://dev.example.com/")
@Issue("AUTH-123")
@TmsLink("TMS-456")
void testAuthentication() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthentication() {
Allure.getLifecycle().updateTestCase(result -> result.setName("Test Authentication"));
Allure.description("This test attempts to log into the website using a login and a password. Fails if any error happens.\n\nNote that this test does not test 2-Factor Authentication.");
Allure.label("severity", "critical");
Allure.label("owner", "John Doe");
Allure.link("Website", "https://dev.example.com/");
Allure.issue("AUTH-123", "https://example.com/issues/AUTH-123");
Allure.tms("TMS-456", "https://example.com/tms/TMS-456");
// ...
}
}
Organize tests
As described in Improving navigation in your test report, Allure supports multiple ways to organize tests into hierarchical structures. Allure JUnit 5 provides functions to assign the relevant fields to tests either by adding annotations or “dynamically” (same as for the metadata fields).
To specify a test's location in the behavior-based hierarchy:
java
import io.qameta.allure.Epic;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
@Epic("Web interface")
@Feature("Essential features")
@Story("Authentication")
void testAuthentication() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthentication() {
Allure.epic("Web interface");
Allure.feature("Essential features");
Allure.story("Authentication");
// ...
}
}
To specify a test's location in the suite-based hierarchy:
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthentication() {
Allure.label("parentSuite" "Tests for web interface");
Allure.suite("Tests for essential features");
Allure.label("subSuite", "Tests for authentication");
// ...
}
}
A test's location in the package-based hierarchy is defined by the fully qualified names of the classes they are declared in, with common prefixes shown as parent packages.
Divide a test into steps
Allure JUnit 5 provides three ways of creating steps and sub-steps: “annotated steps”, “lambda steps” and “no-op steps”, see the reference.
java
import io.qameta.allure.Step;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthentication() {
step1();
step2();
}
@Step("Step 1")
void step1() {
subStep1();
subStep2();
}
@Step("Sub-step 1")
void subStep1() {
// ...
}
@Step("Sub-step 2")
void subStep2() {
// ...
}
@Step("Step 2")
void step2() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthentication() {
Allure.step("Step 1", step -> {
// ...
Allure.step("Sub-step 1");
// ...
Allure.step("Sub-step 2");
});
Allure.step("Step 2", step -> {
// ...
});
}
}
Describe parametrized tests
When using the parametrized tests pattern, use the Allure JUnit 5's @Param
or parameter()
to add the parameters to the report, see the reference.
java
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class TestMyWebsite {
@ParameterizedTest(name = "{displayName} ({argumentsWithNames})")
@ValueSource(strings = {"johndoe", "[email protected]"})
void testAuthentication(String login) {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
class TestMyWebsite {
@Test
void testAuthenticationWithUsername() {
Allure.parameter("login", "johndoe");
// ...
}
@Test
void testAuthenticationWithEmail() {
Allure.parameter("login", "[email protected]");
// ...
}
}
Attach screenshots and other files
You can attach any sorts of files to your Allure report. For example, a popular way to make a report easier to understand is to attach a screenshot of the user interface at a certain point.
Allure JUnit 5 provides various ways to create an attachment, both from existing files or generated dynamically, see the reference.
java
import io.qameta.allure.Allure;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
class TestMyWebsite {
@Test
void testAuthentication() throws IOException {
// ...
Allure.attachment("data.txt", "This is the file content.");
try (InputStream is = Files.newInputStream(Paths.get("/path/img.png"))) {
Allure.attachment("image.png", is);
}
}
}
Select tests via a test plan file
If the ALLURE_TESTPLAN_PATH
environment variable is defined and points to an existing file, JUnit 5 will only run tests listed in this file.
Here's an example of running tests according to a file named testplan.json
:
bash
export ALLURE_TESTPLAN_PATH=testplan.json
./gradlew test
powershell
$Env:ALLURE_TESTPLAN_PATH = "testplan.json"
gradlew test
Environment information
For the main page of the report, you can collect various information about the environment in which the tests were executed.
For example, it is a good idea to use this to remember the OS and Java versions. This may help the future reader investigate bugs that are reproducible only in some environments.
To provide environment information, put a file named environment.properties
into the allure-results
directory after running the tests. See the example in Environment file.
Note that this feature should be used for properties that do not change for all tests in the report. If you have properties that can be different for different tests, consider using Parametrized tests.