- step-by-step instruction
- structure integration - agent. TestNG Listener
- logging integration with LogBack and Log4j
- screenshot attachement
Download this example with:
git clone https://github.com/reportportal/example-java-TestNG.git
- Update
reportportal.properties
file according to you profile page - Navigate to folder with
pom.xml
- call
mvn clean test
Detailed documentation available here:
- https://github.com/reportportal/agent-java-testNG
- http://reportportal.io/docs/Logging-Integration%3Elog4j
This manual will walk you through the steps for integration of Report Portal with TestNG based project
First, make sure you have installed Report Portal, the installation steps could be found here
In this manual we’ll assume that Report Portal is installed and running on http://localhost:8080
If you want to integrate Report Portal with existing project, go to step 3
package com.mycompany.testBase;
public class BaseTest {
// some base steps for all tests
}
package com.mycompany.tests;
import com.mycompany.testBase.BaseTest;
public class MyTests extends BaseTest {
// some steps for tests
}
Here is an example of the project structure you should have
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-epam-reportportal</id>
<name>bintray</name>
<url>http://dl.bintray.com/epam/reportportal</url>
</repository>
</repositories>
The TestNG library
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
Report Portal agent implementation for TestNG
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>agent-java-testng</artifactId>
<version>4.2.1</version>
</dependency>
Latest version of the agent, could be found here
If you prefer using Logback logging library, add following dependencies:
ReportPortal logback logger dependency
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-logback</artifactId>
<version>4.0.0</version>
</dependency>
Up to date version could be found here
The logback itself
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
If you prefer using Log4j logging library, add following dependencies:
ReportPortal log4j logger dependency
<dependency>
<groupId>com.epam.reportportal</groupId>
<artifactId>logger-java-log4j</artifactId>
<version>4.0.1</version>
</dependency>
Up to date version could be found here
The log4j itself
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFilexmlFile>test_suites/MyTestsSuite.xml</suiteXmlFilexmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name> <!-- disabling default listeners is optional -->
<value>false</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
You may also want to use Maven compiler plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
package com.mycompany.tests;
import com.mycompany.testBase.BaseTest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.Test;
public class MyTests extends BaseTest {
private static final Logger LOGGER = LogManager.getLogger(MyTests.class);
@Test
public void testMySimpleTest() {
LOGGER.info("Hello from my simple test");
}
}
Example: ./test_suites/MyTestsSuite.xml
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My Test Suite">
<test name="My Test Testname">
<classes>
<class name="com.mycompany.tests.MyTests"/>
</classes>
</test>
</suite>
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout
pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
</Console>
<ReportPortalLog4j2Appender name="ReportPortalAppender">
<PatternLayout
pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
</ReportPortalLog4j2Appender>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="ReportPortalAppender"/>
</Root>
</Loggers>
</Configuration>
It's needed to add ReportPortalAppender
into this (as shown in the example)
By this moment, your project tree should look somewhat like the this:
Go to http:$IP_ADDRESS_OF_REPORT_PORTAL:8080 (by default it is http://localhost:8080)
Login as Admin user and create the project (more details here and here)
Go to Administrative -> My Test Project -> Members -> Add user
Example link http://localhost:8080/ui/#administrate/project-details/my_test_project/members
Now that you have created new user in your project, you can get reportportal.properties
file example from the user Profile page
To do that, login as created user and go to User icon in header -> Profile
There, in Configuration Examples section, you can find the example of reportportal.properties
file for that user
Returning back to the code. In your project, create file named reportportal.properties
in resources
folder and copy&paste the contents form the user profile page
Example:
rp.endpoint = http://localhost:8080
rp.uuid = 2780ebeb-c976-4aec-a72d-52c71d5fe6f8
rp.launch = john_TEST_EXAMPLE
rp.project = my_test_project
rp.enable = true
More details on
reportportal.properties
file could be found here
Now we need to add ReportPortalTestNGListener
to our TestNG tests.
There are multiple ways for doing that:
- method 1 - using @Listeners annotation
- method 2 (maven only) - using
listener
property in Maven Surefire plugin section inpom.xml
- method 3 - using
listener
section in xml suite file - method 4 - using custom runner
- method 5 - using command line
- method 6 - using ServiceLoader
- method 7 (gradle only) - setting listener in Gradle
test
task
Note: To avoid errors and unpredictable results - make sure to use only one method from the list
Add ReportPortalTestNGListener
to your test class or to the BaseTest
class, which all your tests are supposed to extend
package com.mycompany.testBase;
import com.epam.reportportal.testng.ReportPortalTestNGListener;
import org.testng.annotations.Listeners;
@Listeners({ReportPortalTestNGListener.class})
public class BaseTest {
// some base steps for all tests
}
Make sure your test classes extend the
BaseTest
class
Add the following property to your Maven Surefire plugin in build
section of the pom.xml
<configuration>
<properties>
<property>
<name>listener</name>
<value>com.epam.reportportal.testng.ReportPortalTestNGListener</value>
</property>
</properties>
</configuration>
Pay attention, that if you use this approach, after you run the tests,
the results will be sent to ReportPortal only if you run the tests via maven (e.g. mvn clean test
),
that's when the Maven Surefire plugin is being called to run your tests
Add the following code to your TestNG xml suite file (e.g. ./test_suites/MyTestsSuite.xml
)
<listeners>
<listener class-name="com.epam.reportportal.testng.ReportPortalTestNGListener" />
</listeners>
Pay attention, that if you use this approach, only the results from this suite will be sent to ReportPortal, since the listener is declared only in this suite
Full xml test suite file MyTestsSuite.xml
:
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My Test Suite">
<listeners>
<listener class-name="com.epam.reportportal.testng.ReportPortalTestNGListener" />
</listeners>
<test name="My Test Testname">
<classes>
<class name="com.mycompany.tests.MyTests"/>
</classes>
</test>
</suite>
If you run your tests via custom TestNG runner, you can add listeners to it with the following code:
package com.mycompany.tests;
import com.epam.reportportal.testng.ReportPortalTestNGListener;
import org.testng.*;
import java.util.*;
public class CustomTestNgRunner {
public static void main(String[] args) {
ITestNGListener listener = new ReportPortalTestNGListener();
TestNG testNg = new TestNG();
List<String> lSuites = new ArrayList<String>();
Collections.addAll(lSuites, "test_suites/MyTestsSuite.xml");
testNg.setTestSuites(lSuites);
testNg.addListener(listener);
testNg.run();
}
}
Assuming that you have TestNG and ReportPortal client jars in your class path, you can run TestNG tests with ReportPortal listener as follows:
java org.testng.TestNG ./test_suites/MyTestsSuite.xml –listener com.epam.reportportal.testng.ReportPortalTestNGListener
With ServiceLoader you can override params in run-time.
Example of overriding UUID in run-time:
public class MyListener extends BaseTestNGListener {
public MyListener() {
super(Injector.create(Modules.combine(Modules.override(new ConfigurationModule())
.with(new Module() {
@Override
public void configure(Binder binder) {
Properties overrides = new Properties();
overrides.setProperty(ListenerProperty.UUID.getPropertyName(), "my new uuid");
PropertiesLoader propertiesLoader = PropertiesLoader.load();
propertiesLoader.overrideWith(overrides);
binder.bind(PropertiesLoader.class).toInstance(propertiesLoader);
}
}),
new ReportPortalClientModule(),
new TestNGAgentModule()
)));
}
}
Assuming that you have the default test
task in your gradle.build
file. You can add call of setListeners() methods passing the classpath to ReportPortalTestNGListener
class
test {
testLogging.showStandardStreams = true // enables test log output to STDOUT
useTestNG() { // Tells Gradle to use TestNG
useDefaultListeners = false // Tells TestNG to not use its default reporting structure
Set listeners = ["com.epam.reportportal.testng.ReportPortalTestNGListener"]
setListeners(listeners)
suites 'test_suites/MyTestsSuite.xml' // location of our test suite
}
}
After you added listener using one of the approaches described above, and ran your tests, you should be able to see the results in your ReportPortal instance
To do that, login to ReportPortal, and go to Left Panel
-> Launches
You should see the launch there, with the name, which is the rp.launch
property value from your reportportal.properties
file
Example:
You can also see the suites, test classes and individual test results by clicking on the launch name and going deeper