Manigandan
Manigandan

Reputation: 5082

How to get the Passed, Failed and Skipped method count from the testng suite level

I am overriding the generateReport() of the IReporter interface. To do some action, I need to figure out the total Passed method, Failed method, Skipped method count and total execution time of the testng suite.

I am not sure how to get those counts. Appreciate your help on this. TIA.

Upvotes: 1

Views: 8808

Answers (2)

Vicky
Vicky

Reputation: 3011

You can get the total passed failed and skipped tests using the ITestListener interface

public class TestStatistics implements ITestListener {

List<ITestNGMethod> passedtests = new ArrayList<ITestNGMethod>();
List<ITestNGMethod> failedtests = new ArrayList<ITestNGMethod>();
List<ITestNGMethod> skippedtests = new ArrayList<ITestNGMethod>();


    @Override

    //This method will automatically be called if a test runs successfully
    public void onTestSuccess(ITestResult result) {

       //add the passed tests to the passed list

        passedtests.add(result.getMethod());

    }

    @Override

    //This method will automatically be called if a test fails
    public void onTestFailure(ITestResult result) {

        //add the failed tests to the failed list
        failedtests.add(result.getMethod());

    }

    @Override

     //This method will automatically be called if a test is skipped
     public void onTestSkipped(ITestResult result) {

         //add the skipped tests to the skipped list
        skippedtests.add(result.getMethod());
    }       
}

you can get the passed,failed,skipped tests count by list.size() for respective lists. you can also get the name of the tests passed, failed, skipped from the lists

If you want to get the test execution time then you can use the below methods

    @Override
    //This will be called on after the test class is instantiated
    public void onStart(ITestContext context) {

        context.getStartDate();

    }

    @Override
   //This will be called after the test class run
    public void onFinish(ITestContext context) {

        context.getEndDate();

    }

If you have multiple test classes in a suite then you can add the time of all the test classes which will be your suite run time

Hope this helps you.Kindly get back if you have any queries...

Upvotes: 1

Aru
Aru

Reputation: 134

You can get all information through following code :

 //Iterating over each suite included in the test
      for (ISuite suite : suites) {
       //Following code gets the suite name
        String suiteName = suite.getName();
        //Getting the results for the said suite
          Map<String,ISuiteResult> suiteResults = suite.getResults();
          for (ISuiteResult sr : suiteResults.values()) {

            ITestContext tc = sr.getTestContext();
            System.out.println("Passed tests for suite '" + suiteName +
                 "' is:" + tc.getPassedTests().getAllResults().size());
            System.out.println("Failed tests for suite '" + suiteName +
                 "' is:" + 
                 tc.getFailedTests().getAllResults().size());
            System.out.println("Skipped tests for suite '" + suiteName +
                 "' is:" + 
                 tc.getSkippedTests().getAllResults().size());
            System.out.println("Total excution time for test '" + tc.getName() +
                     "' is:" + (tc.getEndDate().getTime()- tc.getStartDate().getTime()));

For more details,see ITestContext interface.

Upvotes: 2

Related Questions