Skip to content

Commit 58f3e7f

Browse files
committed
Add runtime Launch attributes update example
1 parent e00d652 commit 58f3e7f

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

example-testng-logback/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.epam.reportportal</groupId>
2828
<artifactId>agent-java-testng</artifactId>
29-
<version>5.4.4</version>
29+
<version>5.4.5</version>
3030
</dependency>
3131

3232
<dependency>

example-testng-logback/src/main/java/com/epam/reportportal/example/testng/logback/extension/ParametersAsAttributesTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343
*/
4444
@Listeners({ ParametersAsAttributesTest.ExtendedListener.class })
4545
public class ParametersAsAttributesTest {
46-
4746
private static final Logger LOGGER = LoggerFactory.getLogger(ParametersAsAttributesTest.class);
4847

4948
@Test(threadPoolSize = 2, dataProvider = "bla-bla")
5049
public void testParams(String msg) throws InterruptedException {
5150
for (int i = 0; i < 10; i++) {
52-
LOGGER.info(msg + ": " + i);
51+
LOGGER.info("{}: {}", msg, i);
5352
if (i == 1) {
5453
Thread.sleep(TimeUnit.SECONDS.toMillis(5L));
5554
}
@@ -76,7 +75,6 @@ public void onTestStart(ITestResult testResult) {
7675
}
7776

7877
public static class ParamTaggingTestNgService extends TestNGService {
79-
8078
public ParamTaggingTestNgService(@Nonnull ReportPortal reportPortal) {
8179
super(reportPortal);
8280
}
@@ -91,7 +89,6 @@ protected StartTestItemRQ buildStartStepRq(final @Nonnull ITestResult testResult
9189
attributes.add(new ItemAttributesRQ(null, param.toString()));
9290
}
9391
rq.setAttributes(attributes);
94-
9592
}
9693
return rq;
9794
}

example-testng-logback/src/main/java/com/epam/reportportal/example/testng/logback/extension/RetriedTestReasonReportingTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class RetriedTestReasonReportingTest {
5151
public void failOne() {
5252
int retry = COUNTER.incrementAndGet();
5353
if (retry <= MAXIMUM_RETRIES) {
54-
LOGGER.warn("Failed attempt: " + retry);
54+
LOGGER.warn("Failed attempt: {}", retry);
5555
Assert.fail();
5656
}
5757
LOGGER.info("Success attempt");
@@ -63,14 +63,14 @@ public static class RetryImpl implements IRetryAnalyzer {
6363
@Override
6464
public boolean retry(ITestResult result) {
6565
int retry = retryNumber.incrementAndGet();
66-
LOGGER.info("Retry attempt: " + retry);
66+
LOGGER.info("Retry attempt: {}", retry);
6767
return retry <= MAXIMUM_RETRIES;
6868
}
6969
}
7070

7171
public static class ExtendedListener extends BaseTestNGListener {
72-
public static final Supplier<ITestNGService> SERVICE =
73-
new MemoizingSupplier<>(() -> new SkipReasonLoggingService(ReportPortal.builder().build()));
72+
public static final Supplier<ITestNGService> SERVICE = new MemoizingSupplier<>(() -> new SkipReasonLoggingService(ReportPortal.builder()
73+
.build()));
7474

7575
public ExtendedListener() {
7676
super(SERVICE.get());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.epam.reportportal.example.testng.logback.extension;
2+
3+
import com.epam.reportportal.listeners.ListenerParameters;
4+
import com.epam.reportportal.service.ReportPortal;
5+
import com.epam.reportportal.testng.BaseTestNGListener;
6+
import com.epam.reportportal.testng.ITestNGService;
7+
import com.epam.reportportal.testng.TestNGService;
8+
import com.epam.reportportal.utils.MemoizingSupplier;
9+
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
10+
import com.epam.ta.reportportal.ws.model.attribute.ItemAttributesRQ;
11+
import org.testng.annotations.Listeners;
12+
import org.testng.annotations.Test;
13+
14+
import java.util.Collections;
15+
import java.util.HashSet;
16+
import java.util.Set;
17+
import java.util.concurrent.ConcurrentHashMap;
18+
import java.util.function.Supplier;
19+
20+
import static java.util.Optional.ofNullable;
21+
22+
/**
23+
* Test class to demonstrate how to add runtime attributes to a Launch.
24+
*/
25+
@Listeners({ RuntimeLaunchAttributesTest.ExtendedListener.class })
26+
public class RuntimeLaunchAttributesTest {
27+
private static final Set<ItemAttributesRQ> RUNTIME_ATTRIBUTES = Collections.newSetFromMap(new ConcurrentHashMap<>());
28+
29+
@Test
30+
public void testRuntimeLaunchAttributes() {
31+
System.out.println("Test stub to demonstrate runtime launch attributes.");
32+
// Add runtime attributes
33+
RUNTIME_ATTRIBUTES.add(new ItemAttributesRQ("runtime1", "value1"));
34+
}
35+
36+
public static class ExtendedListener extends BaseTestNGListener {
37+
public static final Supplier<ITestNGService> SERVICE = new MemoizingSupplier<>(() -> new TestNGService(ReportPortal.builder()
38+
.build()) {
39+
@Override
40+
protected FinishExecutionRQ buildFinishLaunchRq(ListenerParameters parameters) {
41+
FinishExecutionRQ rq = super.buildFinishLaunchRq(parameters);
42+
Set<ItemAttributesRQ> attributes = ofNullable(rq.getAttributes()).orElseGet(HashSet::new);
43+
attributes.addAll(RUNTIME_ATTRIBUTES);
44+
rq.setAttributes(attributes);
45+
return rq;
46+
}
47+
});
48+
49+
public ExtendedListener() {
50+
super(SERVICE.get());
51+
}
52+
}
53+
}

example-testng-logback/src/main/java/com/epam/reportportal/example/testng/logback/extension/ScreenshotOnFailureTestExtensionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* More advanced way to report a screenshot. Reports screenshot as a separate log entry in 'Test' method.
3535
*/
36-
@Listeners({ScreenshotOnFailureTestExtensionTest.ExtendedListener.class})
36+
@Listeners({ ScreenshotOnFailureTestExtensionTest.ExtendedListener.class })
3737
public class ScreenshotOnFailureTestExtensionTest {
3838

3939
private static WebDriver driver;

0 commit comments

Comments
 (0)