1 package test.invokedmethodlistener; 2 3 import org.testng.IInvokedMethod; 4 import org.testng.IInvokedMethodListener; 5 import org.testng.ITestResult; 6 7 public class MyListener implements IInvokedMethodListener { 8 private int m_beforeCount = 0; 9 private int m_afterCount = 0; 10 11 private Throwable suiteThrowable; 12 private int suiteStatus = 0; 13 private Throwable methodThrowable; 14 private int methodStatus = 0; 15 16 @Override afterInvocation(IInvokedMethod method, ITestResult testResult)17 public void afterInvocation(IInvokedMethod method, ITestResult testResult) { 18 m_afterCount++; 19 if (method.getTestMethod().isAfterSuiteConfiguration()) { 20 suiteStatus = testResult.getStatus(); 21 suiteThrowable = testResult.getThrowable(); 22 } 23 if (method.getTestMethod().isTest()) { 24 methodStatus = testResult.getStatus(); 25 methodThrowable = testResult.getThrowable(); 26 } 27 } 28 29 @Override beforeInvocation(IInvokedMethod method, ITestResult testResult)30 public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { 31 m_beforeCount++; 32 } 33 getBeforeCount()34 public int getBeforeCount() { 35 return m_beforeCount; 36 } 37 getAfterCount()38 public int getAfterCount() { 39 return m_afterCount; 40 } 41 getSuiteThrowable()42 public Throwable getSuiteThrowable() { 43 return suiteThrowable; 44 } 45 getSuiteStatus()46 public int getSuiteStatus() { 47 return suiteStatus; 48 } 49 getMethodThrowable()50 public Throwable getMethodThrowable() { 51 return methodThrowable; 52 } 53 getMethodStatus()54 public int getMethodStatus() { 55 return methodStatus; 56 } 57 58 } 59