【问题标题】:How to override the index.html report in testNG如何覆盖 testNG 中的 index.html 报告
【发布时间】:2017-11-30 06:38:24
【问题描述】:

我有一个场景,我需要在 index.html testNG 报告中添加一些自定义消息。有什么办法吗?

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

下面的类将创建注解:

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

我只是用谷歌搜索,但不知道数据提供者如何将测试数据发布到默认的 TestNG 报告中。如果有人了解数据提供者的内部逻辑,请告诉我。如果有任何文件可以更好地理解这一点,我们将不胜感激。

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

下面的类将创建注解:

 @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 报告中打印该注释值。

【问题讨论】:

    标签: testng testng-dataprovider


    【解决方案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) 方法进行更改,因为当您单击报告中的时间时,我们将向出现的表中添加成功百分比和测试优先级。

    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" />
    

    然后您将获得如下所示的自定义报告,其中添加了每个测试的成功百分比和优先级

    注意:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多