Java 如何定制testNG中通过/失败测试的报告
我正在尝试使用testNG执行一些测试。测试方法从dataProviderClass接受对象。每当测试运行时,我都要打印对象的名称。我使用了ItestListenrs,但这只是打印测试函数的名称,而不是其中的对象 SampleTest.java: MyListener.java: 因此,目前的输出是通过:SampleTest 但是我希望输出像PASSED:SampleTest\u Object.getStringName一样。尝试以下操作:Java 如何定制testNG中通过/失败测试的报告,java,testng,Java,Testng,我正在尝试使用testNG执行一些测试。测试方法从dataProviderClass接受对象。每当测试运行时,我都要打印对象的名称。我使用了ItestListenrs,但这只是打印测试函数的名称,而不是其中的对象 SampleTest.java: MyListener.java: 因此,目前的输出是通过:SampleTest 但是我希望输出像PASSED:SampleTest\u Object.getStringName一样。尝试以下操作: public class MyListener ext
public class MyListener extends TestListenerAdapter {
@Override
public void onTestSuccess(ITestResult r) {
// Step 1: Find which iteration it is and subtract by 1 to get index
int cnt = r.getMethod().getCurrentInvocationCount() -1 ;
// Step 2: Get the data provider class instance
Class dpc = r.getMethod().getConstructorOrMethod().getMethod().getAnnotation(org.testng.annotations.Test.class).dataProviderClass();
// Step 3: Get the data provider name, which will be method name in dataprovider class.
String dp = r.getMethod().getConstructorOrMethod().getMethod().getAnnotation(org.testng.annotations.Test.class).dataProvider();
// Step 4: Now call the method (it is static method) to get all the objects
try {
Object[][] data = (Object[][]) dpc.getMethod(dp, null).invoke(null);
// Step 5: You have data and index... reach out to the data with which your test was run.
System.out.println("Successfully got the data");
for (Object o: data[cnt]) {
System.out.println(" " + o);
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.abc;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class MyListener extends TestListenerAdapter {
private int m_count = 0;
@Override
public void onTestFailure(ITestResult tr) {
log("Fail");
}
@Override
public void onTestSkipped(ITestResult tr) {
log("Skipped");
}
@Override
public void onTestSuccess(ITestResult tr) {
String className = tr.getMethod().getTestClass().getName();
System.out.println(className);
## WANT TO PRINT HERE THE TESTCASE OBJECT NAME
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
public class MyListener extends TestListenerAdapter {
@Override
public void onTestSuccess(ITestResult r) {
// Step 1: Find which iteration it is and subtract by 1 to get index
int cnt = r.getMethod().getCurrentInvocationCount() -1 ;
// Step 2: Get the data provider class instance
Class dpc = r.getMethod().getConstructorOrMethod().getMethod().getAnnotation(org.testng.annotations.Test.class).dataProviderClass();
// Step 3: Get the data provider name, which will be method name in dataprovider class.
String dp = r.getMethod().getConstructorOrMethod().getMethod().getAnnotation(org.testng.annotations.Test.class).dataProvider();
// Step 4: Now call the method (it is static method) to get all the objects
try {
Object[][] data = (Object[][]) dpc.getMethod(dp, null).invoke(null);
// Step 5: You have data and index... reach out to the data with which your test was run.
System.out.println("Successfully got the data");
for (Object o: data[cnt]) {
System.out.println(" " + o);
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException
| SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}