Allure Integration with TestNG - pragmatictesters/selenium-webdriver-examples GitHub Wiki
Follow the steps below to integrate Allure reporting with your TestNG project.
In your pom.xml
, add the necessary dependencies for Allure and TestNG. These include the Allure TestNG adapter, Allure Maven plugin, and TestNG dependencies.
<dependencies>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
</dependency>
<!-- Allure TestNG Dependency -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.29.1</version>
</dependency>
</dependencies>
In the build
section of your pom.xml
, configure the Allure Maven plugin to generate the Allure reports.
<build>
<plugins>
<!-- Allure Maven Plugin -->
<plugin>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-maven</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>serve</goal> <!-- This serves the report locally -->
</goals>
</execution>
</executions>
</plugin>
<!-- Maven Surefire Plugin (for running the tests) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> <!-- Path to your TestNG suite -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
In the maven-surefire-plugin
configuration, add the system property to specify the directory where Allure should store the results.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<allure.results.directory>target/allure-results</allure.results.directory> <!-- Directory for Allure results -->
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Create a testng.xml
file in the src/test/resources/
folder if you don't already have one. In this file, include the Allure listener to capture test results.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Test Suite">
<listeners>
<listener class-name="io.qameta.allure.testng.AllureTestNg" />
</listeners>
<test name="My Test">
<classes>
<class name="com.example.MyTestClass" /> <!-- Your test class -->
</classes>
</test>
</suite>
- The
testng.xml
file should be located undersrc/test/resources/
. - The listener class
io.qameta.allure.testng.AllureTestNg
ensures that Allure is hooked into your TestNG suite.
In your test class, use Allure annotations to add extra details to your report. For example:
import io.qameta.allure.*;
import org.testng.annotations.Test;
public class MyTestClass {
@Test
@Severity(SeverityLevel.CRITICAL)
@Epic("Epic Name")
@Feature("Feature Name")
@Story("Story Name")
@Description("Test Case Description")
public void testLogin() {
// Test code
System.out.println("Test Execution");
}
}
These annotations add additional metadata to your Allure reports, such as severity, epic, feature, and story.
To run the test and generate the Allure report:
-
Run the TestNG Suite:
mvn clean test
-
Generate Allure Report:
allure serve target/allure-results
The Allure report will be generated and opened in your browser.
By following these steps, you will integrate Allure with TestNG and generate beautiful, interactive test reports. Make sure your pom.xml
is properly configured with the Allure Maven plugin, TestNG dependencies, and correct listener setup in the testng.xml
file.
Happy testing! 🎉