Skip to content

Latest commit

 

History

History
197 lines (148 loc) · 7.81 KB

README_TEMPLATE.md

File metadata and controls

197 lines (148 loc) · 7.81 KB

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.

Maven Central CI Build codecov Join Slack chat! stackoverflow Build with Love

The latest version: $LATEST_VERSION. 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:

  1. Configuration
    • Create/update the reportportal.properties configuration file
    • Build system configuration
    • Add Listener
  2. Logging configuration
    • Loggers and their types
  3. Running tests
    • Build system commands
  4. 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>$LATEST_VERSION</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:$LATEST_VERSION'
}

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:

  1. Create folders /META-INF/services in resources folder (src/main/resources or src/test/resources)
  2. Put there a file named org.testng.ITestNGListener
  3. 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: