下面列出了怎么用org.testng.internal.TestResult的API类实例代码及写法,或者点击链接到github查看源代码。
public void afterInvocation(final IInvokedMethod method, final ITestResult result) {
Reporter.setCurrentTestResult(result);
// Handle Soft CustomAssertion
if (method.isTestMethod()) {
final List<Throwable> verificationFailures = CustomAssertion.getVerificationFailures();
final int size = verificationFailures.size();
if (size == 0) {
return;
} else if (result.getStatus() == TestResult.FAILURE) {
return;
}
result.setStatus(TestResult.FAILURE);
if (size == 1) {
result.setThrowable(verificationFailures.get(0));
} else {
// create failure message with all failures and stack traces barring last failure)
final StringBuilder failureMessage = new StringBuilder("!!! Many Test Failures (").append(size).append(
")\n");
for (int i = 0; i < size - 1; i++) {
failureMessage.append("Failure ").append(i + 1).append(" of ").append(size).append("\n");
final Throwable t = verificationFailures.get(i);
final String fullStackTrace = Utils.stackTrace(t, false)[1];
failureMessage.append(fullStackTrace).append("\n");
}
// final failure
final Throwable last = verificationFailures.get(size - 1);
failureMessage.append("Failure ").append(size).append(" of ").append(size).append(":n");
failureMessage.append(last.toString());
// set merged throwable
final Throwable merged = new Throwable(failureMessage.toString());
merged.setStackTrace(last.getStackTrace());
result.setThrowable(merged);
}
}
}
/**
* Set full test name based on test class, method and other generic information. It is generated based by ITestResult object.
*
* @param ITestResult result
* @return String test name
*/
@SuppressWarnings("unlikely-arg-type")
private static String setTestName(ITestResult result) {
String name = "";
if (result.getTestContext() == null) {
throw new RuntimeException("Unable to set Test name without testContext!");
}
@SuppressWarnings("unchecked")
Map<Object[], String> testnameMap = (Map<Object[], String>) result.getTestContext().getAttribute(SpecialKeywords.TEST_NAME_ARGS_MAP);
if (testnameMap != null) {
String testHash = String.valueOf(Arrays.hashCode(result.getParameters()));
if (testnameMap.containsKey(testHash)) {
name = testnameMap.get(testHash);
}
}
if (name.isEmpty()) {
name = result.getTestContext().getCurrentXmlTest().getName();
}
// TODO: find the bext way to calculate TUID/hash
if (result.getTestContext().getCurrentXmlTest().getAllParameters().containsKey(SpecialKeywords.EXCEL_DS_CUSTOM_PROVIDER) ||
result.getTestContext().getCurrentXmlTest().getAllParameters().containsKey(SpecialKeywords.DS_CUSTOM_PROVIDER)) {
// AUTO-274 "Pass"ing status set on emailable report when a test step fails
String methodUID = "";
for (int i = 0; i < result.getParameters().length; i++) {
if (result.getParameters()[i] != null) {
if (result.getParameters()[i].toString().contains(SpecialKeywords.TUID + ":")) {
methodUID = result.getParameters()[i].toString().replace(SpecialKeywords.TUID + ":", "");
break; // first TUID: parameter is used
}
}
}
if (!methodUID.isEmpty()) {
name = methodUID + " - " + name;
}
}
name = name + " - " + getMethodName(result);
LOGGER.debug("testName: " + name);
// introduce invocation count calculation here as in multi threading mode TestNG doesn't provide valid
// getInvocationCount() value
int index = ((TestResult) result).getParameterIndex();
if (index > 0) {
// that's a dataprovider line index
index++; //to make correlation between line and index number
LOGGER.debug("test: " + name + "; index: " + index);
name = name + String.format(SpecialKeywords.DAPAPROVIDER_INDEX, String.format("%04d", index));
}
ITestNGMethod[] methods = result.getTestContext().getAllTestMethods();
if (methods.length > 0) {
int invCount = methods[0].getInvocationCount();
if (invCount > 1) {
LOGGER.debug("Detected method '" + result.getMethod().getMethodName() + "' with non zero invocationCount: " + invCount);
int countIndex = getCurrentInvocationCount(name);
LOGGER.debug("test: " + name + "; InvCount index: " + countIndex);
name = name + String.format(SpecialKeywords.INVOCATION_COUNTER, String.format("%04d", countIndex));
}
}
testName.set(name);
return testName.get();
}