本文介绍了如何覆盖 testNG 中的 index.html 报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

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

下面的类将创建注释:

 @Retention(RetentionPolicy.RUNTIME)@Target({ 元素类型.方法 })公共@interface 问候{/*** @return - 要打招呼的人的名字.*/String name() 默认"";}

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

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

下面的类将创建注释:

 @Retention(RetentionPolicy.RUNTIME)@Target({ 元素类型.方法 })公共@interface 问候{/*** @return - 要打招呼的人的名字.*/String name() 默认"";

}

下面的类将从用户那里获取数据:

 公共类 TestCase1 {@测试@DataPublish(name="第一个测试方法_1")public static void test1() 抛出异常 {尝试 {断言.assertTrue(真);}捕获(异常前){ex.printStackTrace();}}

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

解决方案

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

为此,您需要更改三个文件(类).所有课程都

注意:

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

I have a scenario where I need to add some custom messages into index.html testNG report. Is there any way to do that?

I just created a custom annotation which I want to publish into index.html testNG report like DataProvider did. I have been tried the below code so far.

The below class will create annotation:

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

I just googled but didn't get any idea about that how dataprovider publish the test data into default TestNG report. If anybody expert about the internal logic of dataprovider please let me know. It would be appreciate if there is any document to understand this better.

I just created a custom annotation which I want to publish into index.html testNG report like DataProvider did. I have been tried the below code so far.

The below class will create annotation:

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

}

The below class will get data from user:

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

I would like to print that annotation value in testNG index.html report.

解决方案

I think you are trying to alter index.html report. You can have any data in the panel classes and print it in index.html.

To achieve this, you need to alter three files (classes). All the classes are here

Main.java

TimesPanel.java (this class will depend on the content (panel) you want to alter. For explaining purpose, we will add content to times panel under info section of report and hence TimesPanel.java )

and BaseMultiSuitePanel.java

create a file customBaseMultiSuitePanel.java in your project , copy the content of original file and make change to constructor accordingly.

create customTimesPanel.java and copy the content of TimesPanel.java and make changes to private String js(ISuite suite) method as we are going to add successPercentage and priority of tests to the table that comes up , when you click on times in report.

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++;
          ...
          ...
        }

Next, create customIndexHtmlReport.java and copy the contentof Main.java in this file. Remove the old TimesPanel object and the new like below in public void generateReport() method

 new customTimesPanel(m_model)

Also change the report name in the same file like this

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

And finally, add listener to your testng.xml

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

then you will get your custom report like below with added successPercentage and priority for each test

Note:

make sure resources pertaining to getClass().getResourceAsStream method in customIndexHtmlReport.java is properly placed in your project. I had issues with it.

这篇关于如何覆盖 testNG 中的 index.html 报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:04