Skip to content

Commit

Permalink
Common Stack Trace frames skip in description and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Nov 11, 2024
1 parent f326198 commit 9fd7acf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Added
- Common Stack Trace frames skip in description and logs, by @HardNorth
### Changed
- Client version upgraded on [5.2.20](https://github.com/reportportal/client-java/releases/tag/5.2.20), by @HardNorth

Expand Down
66 changes: 36 additions & 30 deletions src/main/java/com/epam/reportportal/testng/TestNGService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import io.reactivex.Maybe;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.testng.*;
import org.testng.annotations.Factory;
Expand All @@ -65,11 +64,11 @@
import java.util.stream.Stream;

import static com.epam.reportportal.testng.util.ItemTreeUtils.createKey;
import static com.epam.reportportal.utils.formatting.ExceptionUtils.getStackTrace;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;

/**
* TestNG service implements operations for interaction ReportPortal
Expand Down Expand Up @@ -299,6 +298,21 @@ public void startConfiguration(ITestResult testResult) {
testResult.setAttribute(RP_ID, itemID);
}

/**
* Extension point to customize test step description
*
* @param testResult TestNG's testResult context
* @return Test/Step Description being sent to ReportPortal
*/
@Nonnull
protected String createStepDescription(@Nonnull ITestResult testResult) {
var methodDescriptionOptional = getMethodAnnotation(Description.class, testResult);
if (methodDescriptionOptional.isPresent()) {
return methodDescriptionOptional.get().value();
}
return testResult.getMethod().getDescription();
}

/**
* Extension point to customize test step creation event/request
*
Expand Down Expand Up @@ -364,6 +378,25 @@ public void startTestMethod(@Nonnull ITestResult testResult) {
}
}

/**
* Extension point to customize test step description with error message
*
* @param testResult TestNG's testResult context
* @return Test/Step Description being sent to ReportPortal
*/
@Nullable
private String getLogMessage(@Nonnull ITestResult testResult) {
String error = ofNullable(testResult.getThrowable()).map(t -> String.format(
DESCRIPTION_ERROR_FORMAT,
getStackTrace(t, new Throwable())
)).orElse(null);
if (error == null) {
return null;
}
String description = createStepDescription(testResult);
return StringUtils.isNotBlank(description) ? MarkdownUtils.asTwoParts(description, error) : error;
}

/**
* Extension point to customize test method on it's finish
*
Expand Down Expand Up @@ -494,7 +527,7 @@ public void sendReportPortalMsg(final ITestResult result) {
rq.setItemUuid(itemUuid);
rq.setLevel("ERROR");
if (result.getThrowable() != null) {
rq.setMessage(getStackTrace(result.getThrowable()));
rq.setMessage(getStackTrace(result.getThrowable(), new Throwable()));
} else {
rq.setMessage("Test has failed without exception");
}
Expand Down Expand Up @@ -718,20 +751,6 @@ protected String createStepName(ITestResult testResult) {
return testStepName == null ? testResult.getMethod().getMethodName() : testStepName;
}

/**
* Extension point to customize test step description
*
* @param testResult TestNG's testResult context
* @return Test/Step Description being sent to ReportPortal
*/
protected String createStepDescription(ITestResult testResult) {
var methodDescriptionOptional = getMethodAnnotation(Description.class, testResult);
if (methodDescriptionOptional.isPresent()) {
return methodDescriptionOptional.get().value();
}
return testResult.getMethod().getDescription();
}

@Nullable
private TestCaseIdEntry getTestCaseId(@Nonnull String codeRef, @Nonnull ITestResult testResult) {
Method method = getMethod(testResult);
Expand Down Expand Up @@ -801,17 +820,4 @@ Maybe<String> getConfigParent(ITestResult testResult, TestMethodType type) {
}
return parentId;
}

/**
* Extension point to customize test step description with error message
*
* @param testResult TestNG's testResult context
* @return Test/Step Description being sent to ReportPortal
*/
private String getLogMessage(ITestResult testResult) {
String error = String.format(DESCRIPTION_ERROR_FORMAT, ExceptionUtils.getStackTrace(testResult.getThrowable())).trim();
return ofNullable(createStepDescription(testResult)).filter(StringUtils::isNotBlank)
.map(description -> MarkdownUtils.asTwoParts(description, error))
.orElse(error);
}
}

0 comments on commit 9fd7acf

Please sign in to comment.