如何覆盖testNG中的index.html报告

时间:2017-11-29 05:58:27

标签: testng testng-dataprovider

我有一个场景,我需要在index.html testNG报告中添加一些自定义消息。有没有办法做到这一点?

我刚创建了一个自定义注释,我希望将其发布到index.html testNG报告中,就像DataProvider一样。到目前为止,我已尝试过以下代码。

以下类将创建注释:

     @Retention(RetentionPolicy.RUNTIME)
     @Target({ ElementType.METHOD })
     public @interface Greet {
        /**
         * @return - The name of the person to greet.
         */
        String name() default "";
     }

我只是用Google搜索,但没有想到dataprovider如何将测试数据发布到默认的TestNG报告中。如果有任何关于数据提供者内部逻辑的专家请告诉我。如果有任何文件可以更好地理解这一点,将不胜感激。

我刚创建了一个自定义注释,我希望将其发布到index.html testNG报告中,就像DataProvider一样。到目前为止,我已尝试过以下代码。

以下类将创建注释:

 @Retention(RetentionPolicy.RUNTIME)
 @Target({ ElementType.METHOD })
 public @interface Greet {
    /**
     * @return - The name of the person to greet.
     */
    String name() default "";

}

以下课程将从用户处获取数据:

  public class TestCase1 {
    @Test
    @DataPublish(name="First Test method_1")
    public static void test1() throws Exception {
       try {
            Assert.assertTrue(true);
           } 
       catch (Exception ex) {
            ex.printStackTrace();
        }
     }

我想在testNG index.html报告中打印该注释值。

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试更改index.html报告。您可以在面板类中包含任何数据,并将其打印在index.html中。

要实现此目的,您需要更改三个文件(类)。所有课程都是here

Main.java

TimesPanel.java(此类将取决于您要更改的内容(面板)。为了解释目的,我们将在报告的信息部分下添加内容到时间面板,因此TimesPanel.java

BaseMultiSuitePanel.java

在项目中创建文件customBaseMultiSuitePanel.java,复制原始文件的内容并相应地更改构造函数。

创建customTimesPanel.java并复制TimesPanel.java的内容并对private String js(ISuite suite)方法进行更改,因为我们将在您点击时将successPercentage和测试优先级添加到出现的表中在报告的时间。

public class customTimesPanel extends customBaseMultiSuitePanel {

...
...

      private String js(ISuite suite) {
        String functionName = "tableData_" + suiteToTag(suite);
        StringBuilder result = new StringBuilder(
            "suiteTableInitFunctions.push('" + functionName + "');\n"
              + "function " + functionName + "() {\n"
              + "var data = new google.visualization.DataTable();\n"
              + "data.addColumn('number', 'Number');\n"
              + "data.addColumn('string', 'Method');\n"
              + "data.addColumn('string', 'Class');\n"
              + "data.addColumn('number', 'Time (ms)');\n"
              + "data.addColumn('string', 'SuccessPercentage');\n"
              + "data.addColumn('string', 'Priority');\n");

        List<ITestResult> allTestResults = getModel().getAllTestResults(suite);
        result.append(
          "data.addRows(" + allTestResults.size() + ");\n");

        Collections.sort(allTestResults, new Comparator<ITestResult>() {
          @Override
          public int compare(ITestResult o1, ITestResult o2) {
            long t1 = o1.getEndMillis() - o1.getStartMillis();
            long t2 = o2.getEndMillis() - o2.getStartMillis();
            return (int) (t2 - t1);
          }
        });

        int index = 0;
        for (ITestResult tr : allTestResults) {
          ITestNGMethod m = tr.getMethod();
          long time = tr.getEndMillis() - tr.getStartMillis();
          result
              .append("data.setCell(" + index + ", "
                  + "0, " + index + ")\n")
              .append("data.setCell(" + index + ", "
                  + "1, '" + m.getMethodName() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "2, '" + m.getTestClass().getName() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "3, " + time + ")\n")
              .append("data.setCell(" + index + ", "
                  + "4, '" + m.getSuccessPercentage() + "')\n")
              .append("data.setCell(" + index + ", "
                  + "5, '" + m.getPriority() + "');\n"); 
          Long total = m_totalTime.get(suite.getName());
          if (total == null) {
            total = 0L;
          }
          m_totalTime.put(suite.getName(), total + time);
          index++;
          ...
          ...
        }

接下来,创建customIndexHtmlReport.java并在此文件中复制Main.java的内容。在public void generateReport()方法

中删除旧的TimesPanel对象和下面的新对象
 new customTimesPanel(m_model) 

同时更改同一文件中的报告名称

Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all); 

最后,将监听器添加到testng.xml

<listener class-name = "firsttestngpackage.customIndexHtmlReport" />

然后您将获得如下所示的自定义报告,并为每项测试添加successPercentage和优先级

enter image description here

注意:

确保getClass().getResourceAsStream中与customIndexHtmlReport.java方法相关的资源正确放置在您的项目中。我遇到了问题。

相关问题