Appearance
Getting started with Allure TestNG
Generate beautiful HTML reports using Allure Report and your TestNG tests.
INFO
Check out the example projects at github.com/allure-examples to see Allure TestNG in action.
Setting up
To integrate Allure into an existing TestNG 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.24.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-testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
kts
// Define the version of Allure you want to use via the allureVersion property
val allureVersion = "2.24.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-testng")
}
groovy
// Define the version of Allure you want to use via the allureVersion property
def allureVersion = "2.24.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-testng"
}
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.20.1</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.1.2</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.20.1"
// 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.20.1"
// 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 TestNG 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 TestNG adapter not only collects the data provided by TestNG'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 TestNG's and Allure TestNG's features.
With Allure TestNG, 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.testng.annotations.Test;
import static io.qameta.allure.SeverityLevel.*;
public class TestMyWebsite {
@Test
@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")
public void testAuthentication() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.testng.annotations.Test;
public class TestMyWebsite {
@Test
public void testAuthentication() {
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 TestNG 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.testng.annotations.Test;
public class TestMyWebsite {
@Test
@Epic("Web interface")
@Feature("Essential features")
@Story("Authentication")
public void testAuthentication() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.testng.annotations.Test;
public class TestMyWebsite {
@Test
public 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.testng.annotations.Test;
public class TestMyWebsite {
@Test
public 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 TestNG 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.testng.annotations.Test;
public class TestMyWebsite {
@Test
public void testAuthentication() {
step1();
step2();
}
@Step("Step 1")
public void step1() {
subStep1();
subStep2();
}
@Step("Sub-step 1")
public void subStep1() {
// ...
}
@Step("Sub-step 2")
public void subStep2() {
// ...
}
@Step("Step 2")
public void step2() {
// ...
}
}
java
import io.qameta.allure.Allure;
import org.testng.annotations.Test;
public class TestMyWebsite {
@Test
public 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
Allure TestNG provides complete support for TestNG's parameterized tests.
See the reference for more examples.
java
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestMyWebsite {
@Test
@Parameters({"Login", "Password"})
public void testAuthentication(String login, String password) {
// ...
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test my website">
<test name="Test authentication with login">
<parameter name="Login" value="johndoe"/>
<parameter name="Password" value="qwerty"/>
<classes>
<class name="com.example.TestMyWebsite"/>
</classes>
</test>
<test name="Test authentication with email">
<parameter name="Login" value="[email protected]"/>
<parameter name="Password" value="qwerty"/>
<classes>
<class name="com.example.TestMyWebsite"/>
</classes>
</test>
</suite>
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 TestNG 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.testng.annotations.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class TestMyWebsite {
@Test
public 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, TestNG 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.