如何在testNG中重写index.html报告

如何在testNG中重写index.html报告,testng,testng-dataprovider,Testng,Testng Dataprovider,我有一个场景,需要在index.html testNG报告中添加一些自定义消息。有办法吗 我刚刚创建了一个自定义注释,希望像DataProvider一样发布到index.html testNG报告中。到目前为止,我已经尝试了以下代码 下面的类将创建注释: @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) public @interface Greet { /**

我有一个场景,需要在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 "";
     }
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ ElementType.METHOD })
 public @interface Greet {
    /**
     * @return - The name of the person to greet.
     */
    String name() default "";
我只是在谷歌上搜索了一下,但不知道dataprovider是如何将测试数据发布到默认的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 "";
     }
 @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报告中打印该注释值。

我想您正在尝试更改index.html报告。您可以在面板类中拥有任何数据,并在index.html中打印它

要实现这一点,您需要修改三个文件(类)。所有的课程都是

Main.java

TimesPanel.java
(该类将取决于您要更改的内容(面板)。为了便于解释,我们将在报告的“信息”部分的“时代”面板中添加内容,从而
TimesPanel.java

BaseMultiSuitePanel.java

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

创建
customTimesPanel.java
并复制
TimesPanel.java
的内容,并对
private String js(ISuite)
方法进行更改,因为我们将在单击报告中的时间时向出现的表中添加测试的成功百分比和优先级

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++;
          ...
          ...
        }
还要像这样更改同一文件中的报告名称

Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all); 
最后,将侦听器添加到
testng.xml

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

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

注意:

确保与
customIndexHtmlReport.java
中的
getClass().getResourceAsStream
方法相关的资源正确放置在项目中。我对此有意见