如何从testng套件级别获取Passed,Failed和Skipped方法计数

时间:2015-06-06 11:12:45

标签: selenium selenium-webdriver automation testng

我正在覆盖IReporter接口的generateReport()。要做一些动作,我需要弄清楚测试套件的总Passed方法,Failed方法,Skipped方法计数和总执行时间。

我不知道如何获得这些重要性。感谢您对此的帮助。 TIA。

2 个答案:

答案 0 :(得分:1)

您可以通过以下代码获取所有信息:

 //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()));

有关详细信息,请参阅ITestContext界面。

答案 1 :(得分:0)

您可以使用ITestListener接口

获取总传递失败和跳过测试
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());
    }       
}

您可以通过list.size()获取相应列表的传递,失败,跳过测试计数。   你也可以从列表中获取传递,失败,跳过的测试的名称

如果您想获得测试执行时间,那么您可以使用以下方法

    @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();

    }

如果套件中有多个测试类,那么您可以添加所有测试类的时间,这将是您的套件运行时间

希望这会对你有所帮助。如果你有任何疑问,请回来......