我已经使用onTestSuccess,OnTestFailure来测试用例执行结果。我能够报告我的测试用例是通过还是失败。
if (result.getStatus() == ITestResult.SUCCESS) {
//
} else if (result.getStatus() == ITestResult.FAILURE) {
//
}else if (result.getStatus() == ITestResult.SKIP) {
//
}
但我还需要分别捕获每个方法的日志..这不应该与其他线程日志重叠。
@Test
public void test001() throws IOException {
System.out.println("Test001");
}
@Test
public void test002() throws IOException {
System.out.println("Test001");
}
有人可以提供帮助或建议吗?
答案 0 :(得分:0)
是的,你可以很容易地做到这一点。但唯一需要注意的是,您需要访问测试方法的ITestResult
对象(@Test
方法)来获取其日志。您需要做的就是使用Reporter.log()
记录消息,然后使用Reporter.getOutput()
来检索日志。
这是一个展示此功能的示例。
以下是测试类的外观。
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);
}
}
这是一个检索日志的监听器
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);
}
}
以下是套件xml文件的外观:
<?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>
这是控制台输出
... 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
答案 1 :(得分:0)
您也可以AfterMethod
TestNG
检索日志,就像明智一样:
@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("");
}
}
其中testStatus是用户定义的函数,用于处理Test方法的每个通过/失败状态,该状态已在AfterMethod
上调用,因此在完成每个测试后,它将通过并返回Status。