agent-java-testNG
agent-java-testNG copied to clipboard
TestNG listener
ReportPortal Listener for TestNG tests
A TestNG reporter that uploads the results to a ReportPortal server.
DISCLAIMER: We use Google Analytics for sending anonymous usage information such as agent's and client's names, and their versions after a successful launch start. This information might help us to improve both ReportPortal backend and client sides. It is used by the ReportPortal team only and is not supposed for sharing with 3rd parties.
The latest version: 5.4.2. Please use Maven Central link above to get the agent.
For TestNG version 7.1.0 and higher
Overview: How to Add ReportPortal Logging to Your Project
To start using ReportPortal with TestNG framework please do the following steps:
- Configuration
- Create/update the
reportportal.propertiesconfiguration file - Build system configuration
- Add Listener
- Create/update the
- Logging configuration
- Loggers and their types
- Running tests
- Build system commands
- Custom use examples
Configuration
'reportportal.properties' configuration file
To start using ReportPortal you need to create a file named reportportal.properties in your Java project in a source
folder src/main/resources or src/test/resources (depending on where your tests are located):
reportportal.properties
rp.endpoint = http://localhost:8080
rp.api.key = e0e541d8-b1cd-426a-ae18-b771173c545a
rp.launch = TestNG Tests
rp.project = default_personal
Property description
rp.endpoint- the URL for the ReportPortal server (actual link).rp.api.key- an access token for ReportPortal which is used for user identification. It can be found on your report portal user profile page.rp.project- a project ID on which the agent will report test launches. Must be set to one of your assigned projects.rp.launch- a user-selected identifier of test launches.
The full list of supported properties is located here in client-java library documentation (a common library for all Java agents): https://github.com/reportportal/client-java
Build system configuration
Maven
If your project is Maven-based you need to add dependencies to pom.xml file:
<project>
<!-- project declaration omitted -->
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>agent-java-testng</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<!-- build config omitted -->
</project>
You are free to use you own version of TestNG, but not earlier than 7.1.0. If you leave just Agent dependency it will be still OK, it will use transitive TestNG version.
Gradle
For Gradle-based projects please update dependencies section in build.gradle file:
dependencies {
testImplementation 'com.epam.reportportal:agent-java-testng:5.4.2'
}
Listener configuration
There are many ways to configure a listener in TestNG, but the most elegant and recommended way is to use a
ServiceLoader file. Here is how you can do that:
- Create folders /META-INF/services in resources folder (
src/main/resourcesorsrc/test/resources) - Put there a file named org.testng.ITestNGListener
- Put a default implementation reference as a single row into the file: com.epam.reportportal.testng.ReportPortalTestNGListener
Example: /META-INF/services/org.testng.ITestNGListener
com.epam.reportportal.testng.ReportPortalTestNGListener
That's it! You are all set.
Logging
ReportPortal provides its own logger implementations for major logging frameworks like Log4j and Logback. It also provides additional formatting features for popular client and test libraries like: Selenide, Apache HttpComponents, Rest Assured, etc.
Here is the list of supported loggers and setup documentation links.
Logging frameworks:
| Library name | Documentation link |
|---|---|
| Log4j | https://github.com/reportportal/logger-java-log4j |
| Logback | https://github.com/reportportal/logger-java-logback |
HTTP clients:
| Library name | Documentation link |
|---|---|
| OkHttp3 | https://github.com/reportportal/logger-java-okhttp3 |
| Apache HttpComponents | https://github.com/reportportal/logger-java-httpcomponents |
Test frameworks:
| Library name | Documentation link |
|---|---|
| Selenide | https://github.com/reportportal/logger-java-selenide |
| Rest Assured | https://github.com/reportportal/logger-java-rest-assured |
Running tests
We are set. To run tests we just need to execute corresponding command in our build system.
Maven
mvn test or mvnw test if you are using Maven wrapper
Gradle
gradle test or gradlew test if you are using Gradle wrapper
Customization
Code example How to overload params in run-time
As a sample you can use code for Override UUID in run-time
public static class MyListener extends BaseTestNGListener {
public MyListener() {
super(new ParamOverrideTestNgService());
}
}
public static class ParamOverrideTestNgService extends TestNGService {
public ParamOverrideTestNgService() {
super(getLaunchOverriddenProperties());
}
private static Supplier<Launch> getLaunchOverriddenProperties() {
ListenerParameters parameters = new ListenerParameters(PropertiesLoader.load());
parameters.setApiKey("my crazy uuid");
ReportPortal reportPortal = ReportPortal.builder().withParameters(parameters).build();
StartLaunchRQ rq = buildStartLaunch(reportPortal.getParameters());
return new Supplier<Launch>() {
@Override
public Launch get() {
return reportPortal.newLaunch(rq);
}
};
}
private static StartLaunchRQ buildStartLaunch(ListenerParameters parameters) {
StartLaunchRQ rq = new StartLaunchRQ();
rq.setName(parameters.getLaunchName());
rq.setStartTime(Calendar.getInstance().getTime());
rq.setAttributes(parameters.getAttributes());
rq.setMode(parameters.getLaunchRunningMode());
if (!Strings.isNullOrEmpty(parameters.getDescription())) {
rq.setDescription(parameters.getDescription());
}
return rq;
}
}
Example repository
There are two modules under Example project which represent agent usage with Lo4j and Logback loggers:
- https://github.com/reportportal/examples-java/tree/master/example-testng-log4j
- https://github.com/reportportal/examples-java/tree/master/example-testng-logback