Giter VIP home page Giter VIP logo

agent-java-testng's Introduction

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: 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:

  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>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:

  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:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.