0

I'm trying to categorize TestNG failures based on the type of exception/error causing the error. Is there some way to do this? I'm relatively new to TestNG, so would appreciate any help possible

Vijeet Vergis
  • 101
  • 1
  • 10

1 Answers1

2

Yes you can do this by following the below overall approach.

  • Build a custom reporter implementation that implements the TestNG interface org.testng.IReporter
  • Within this interface implementatation wherein you will get access to ITestResult object which represents the results of a test method and then examine its exception via org.testng.ITestResult#getThrowable and then include your logic to classify failures.

Here's a draft implementation

public class SampleReporter implements IReporter {
    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        for (ISuite suite : suites) {
            Map<String, ISuiteResult> suiteResults = suite.getResults();
            for (ISuiteResult sr : suiteResults.values()) {
                ITestContext tc = sr.getTestContext();
                Set<ITestResult> failedResults = tc.getFailedTests().getAllResults();
                for (ITestResult failedResult : failedResults) {
                    Throwable throwable = failedResult.getThrowable();
                    if (throwable instanceof WebDriverException) {
                        //classify this as a selenium exception
                    }
                }
            }
        }
    }
}

You can now decide to wire in this listener using one of the below options

  • Using the @Listeners annotation on your test class.
  • Using the <listeners> tag in your suite xml file
  • Using the ServiceLoader approach

You can refer to this blog post of mine to learn more about TestNG listeners in general and all the above listed ways of listener injection.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66