1

I'm trying to implement org.testng IReporter Interface. My Java is not that great, base on some example I found online I was able to create a Reporter class. The problem I'm having is how to use it and where do I call it and how and which parameters to pass to it?

public class Reporter implements IReporter {

public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory){
      ISuiteResult results =suites.get(0).getResults().get("Sanity Suite");
      ITestContext context = results.getTestContext();

      IResultMap passedTests = context.getPassedTests();
      IResultMap failedTests = context.getFailedTests();

      // Print all test exceptions...
      for( ITestResult r: failedTests.getAllResults()) {
          System.out.println( r.getThrowable());
      }
}

}

For example I have this WebDriver Selenium TestNG test:

public class VerifyTest extends TestBase {

@Test
public void test1() {
    verifyTrue(false);
    verifyEquals("pass", "fail");
    verifyFalse(true);
}

@Test
public void test2() {
    verifyTrue(false);
    assertEquals("pass", "fail");
    verifyFalse(true);
}

How would I use my Reporter to get a customize report at the end of the run???

Thank You!

4

5 回答 5

1

大卫,您可以将自定义报告器添加到您的 testng.xml 中,以防您通过套件部分中的 xml 调用测试。

<listeners>
<listener class-name="yourpackage.Reporter"/> </listeners>

如果您以编程方式调用它们,那么您需要通过您的代码添加它,如@Running TestNG programmatically所记录的那样

如果您从命令行调用测试,请参阅

如果您以上述任何一种方式指定,TestNG 将在所有运行结束时调用此报告器。

于 2012-04-19T05:05:14.033 回答
1

只需在上面扩展您的骨架即可在您想要的位置生成结果,.xml、.html、文本文件等...

于 2012-04-19T01:50:45.370 回答
1

创建了一个项目,它是生成自定义报告的示例

基本思想是创建一个Listener类并在testing.xml文件中引用它。

<listeners>
    <listener class-name="qa.hs.framework.CustomReportListener"/>
  </listeners>

然后创建类:

 public class CustomReportListener implements IReporter {

    @Override
    public void generateReport( List<XmlSuite> xmlSuites, List<ISuite> suites, 
           String outputDirectory ) {
       System.out.println();
       //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());
        }
        CustomReport cr = new CustomReport();
        cr.generateReport( xmlSuites, suites, outputDirectory );
        ...

然后,从该 Listener 类中,您可以创建一个“报告编写器”类,该类可以使用以下内容创建任意 HTML 输出:

public class CustomReport extends CustomReportListener
{
        private static final Logger LOG = Logger.getLogger( CustomReport.class );
        private static final SimpleDateFormat dateFormatter = new SimpleDateFormat(" MMM d 'at' hh:mm a");

        private String reportFileName = Constants.reportFileName;
        private PrintWriter m_out;
        private int m_row;
        private Integer m_testIndex;
        private int m_methodIndex;
        private Scanner scanner;

        @Override
        public void generateReport( List<XmlSuite> xml, List<ISuite> suites, String outdir ) {
                try {
                        m_out = createWriter( outdir );
                }
                catch ( IOException e ) {
                        LOG.error("output file", e);
                        return;
                }
                startHtml(m_out);
                generateSuiteSummaryReport(suites);
                generateMethodSummaryReport(suites);
                generateMethodDetailReport(suites);
                endHtml(m_out);
                m_out.flush();
                m_out.close();
        }

最后,从“CustomReport”类中,您的“生成报告”方法都可以访问报告中的所有数据,例如:

testContext.getPassedTests()
Map<String, ISuiteResult> r = suite.getResults()
method.getDescription()
method.getTestClass().getName()
ITestResult.SUCCESS
tests.getAllMethods()
overview.getStartDate().getTime()
overview.getIncludedGroups()
etc.
于 2014-01-21T05:43:43.290 回答
1

使用实时报告插件可以实时报告任何正在运行的测试的漂亮报告。非常易于使用,无需修改现有代码,详情请访问此 github url RealTimeReport

您可以找到不同 TestNg 报告接口的详细实现

在此处输入图像描述

于 2016-10-08T17:38:53.790 回答
1

您可以使用

@Listeners(ReporterClassName.class) 

例如:@Listeners(Reporter.class)

公共类 VerifyTest 扩展了 TestBase

于 2015-12-16T08:11:37.307 回答