问题
I have used onTestSuccess, OnTestFailure for Test case execution results. I am able to report my Test case is pass or fail.
if (result.getStatus() == ITestResult.SUCCESS) {
//
} else if (result.getStatus() == ITestResult.FAILURE) {
//
}else if (result.getStatus() == ITestResult.SKIP) {
//
}
But I also need to capture logs for each method separately.. that should not overlapping with other thread logs.
@Test
public void test001() throws IOException {
System.out.println("Test001");
}
@Test
public void test002() throws IOException {
System.out.println("Test001");
}
Can someone help or suggestions ?
回答1:
Yes you can do it very easily. But the only caveat is that you would need access to the ITestResult
object of a test method (@Test
method) to get hold of its logs. All you need to do is, use Reporter.log()
to log messages and then use Reporter.getOutput()
to retrieve the logs.
Here's a sample that shows this in action.
Here's how the test class would look like.
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(TestCaseLogPrinter.class)
public class TestClassSample {
@Test
public void test001() {
Reporter.log("Test001 : This is first message", true);
Reporter.log("Test001 : This is second message", true);
}
@Test
public void test002() {
Reporter.log("Test002 : This is a random message", true);
Reporter.log("Test002 : This is another random message", true);
}
}
Here's a listener that retrieves the logs
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.List;
public class TestCaseLogPrinter extends TestListenerAdapter {
@Override
public void onTestSuccess(ITestResult tr) {
System.err.println("Printing the test method logs " + asString(Reporter.getOutput(tr)));
}
private String asString(List<String> output) {
StringBuilder builder = new StringBuilder();
for (String each : output) {
builder.append(each).append(", ");
}
//Removing the last ","
return builder.toString().substring(0, builder.length() - 2);
}
}
Here's how the suite xml file looks like :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="49493003_Suite" parallel="methods" verbose="2">
<test name="49493003_test" verbose="2">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn49493003.TestClassSample"/>
</classes>
</test>
</suite>
And here's the console output
... TestNG 6.14.3 by Cédric Beust (cedric@beust.com)
...
Test001 : This is first message
Test002 : This is a random message
Test001 : This is second message
Test002 : This is another random message
Printing the test method logs Test001 : This is first message, Test001 : This is second message
Printing the test method logs Test002 : This is a random message, Test002 : This is another random message
PASSED: test001
PASSED: test002
===============================================
49493003_test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
49493003_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
回答2:
You can also retrieve log by AfterMethod
of TestNG
, like wise :
@AfterMethod
public void calltestStatus(ITestResult result) throws IOException
{
testStatus(result);
}
public void testStatus(ITestResult result) throws IOException
{
if (result.getStatus() == ITestResult.FAILURE) {
System.out.println("");
} else if (result.getStatus() == ITestResult.SUCCESS) {
System.out.println("");
} else if (result.getStatus() == ITestResult.SKIP) {
System.out.println("");
} else {
System.out.println("");
}
}
Where testStatus is user defined function which handle each and every pass/fail status of Test method, which has been called on AfterMethod
so after completing each Test it will come through and return Status.
来源:https://stackoverflow.com/questions/49493003/testng-get-parallel-execution-method-logs-separately