有没有办法可以打印在@aftersuite中执行的失败,通过,总测试。
我知道这可以覆盖ITestListener但是有没有直接的方法可以在@aftersuite中得到重视?
提前感谢您的帮助。
答案 0 :(得分:0)
下面的示例应该说明如何做到这一点。
import org.testng.Assert;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;
import java.util.Collection;
public class TestClassSample {
@Test
public void passingTestMethod() { }
@Test
public void failingTestMethod() {
Assert.fail();
}
@AfterSuite
public void afterSuite(ITestContext context) {
printSuiteResults(context.getSuite());
}
private void printSuiteResults(ISuite suite) {
Collection<ISuiteResult> suiteResults = suite.getResults().values();
for (ISuiteResult suiteResult : suiteResults) {
printAllResults(suiteResult.getTestContext());
}
}
private void printAllResults(ITestContext context) {
System.err.println("Printing tests that passed.");
printAllResults(context.getPassedTests().getAllResults());
System.err.println("Printing tests that failed.");
printAllResults(context.getFailedTests().getAllResults());
}
private void printAllResults(Collection<ITestResult> results) {
for (ITestResult result : results) {
printResult(result);
}
}
private void printResult(ITestResult result) {
System.err.println("Method Name: " + result.getMethod().getMethodName() + "()");
}
}
这是输出:
java.lang.AssertionError: null
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert.fail(Assert.java:100)
at com.rationaleemotions.stackoverflow.qn46967666.TestClassSample.failingTestMethod(TestClassSample.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:426)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:421)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:386)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Printing tests that passed.
Method Name: passingTestMethod()
Printing tests that failed.
Method Name: failingTestMethod()
===============================================
Default Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================