Dataprovider 如何将测试数据发布到 HTML 报告中 [英] How Dataprovider publish the test data into HTML report

查看:19
本文介绍了Dataprovider 如何将测试数据发布到 HTML 报告中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面的类将创建注释:

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

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

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

我想在 testNG 默认 HTML 报告中打印该注释值.

解决方案

你的数据提供者可以为任何类或方法提供数据,我相信那里有这样的例子.您可以在下面的课程中添加您的数据.我已经在下面解释了自定义报告部分.

使用你的 customReport 你必须实现 IReporter ,扩展 TestListenerAdapter 并覆盖 generateReport 方法,如果你想要实现自定义

现在,您将获得两份报告,一份为默认值,另一份为您的文件名.现在唯一剩下的就是关闭默认报告侦听器,因此您只能获得一份报告.为此,您可以按照 this 或者您可以搜索解决方案.希望这有帮助

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 default testNG HTML 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 default HTML report.

解决方案

Your dataprovider can provide data to any of the class or methods ,I am sure there are examples for that out there. You can add your data in the below class. I have explained the custom reporting part below.

With your customReport You'd have to implement IReporter , extend TestListenerAdapter and override generateReport method if you want to implement a custom TestHTMLReporter . For other reporters you may have to do things a bit differently but the concept will remain the same. You'd achieve custom 'TestHTMLReporter' like below .

Create a CustomReport.java file in your project and copy-paste the whole content of TestHTMLReporter.java , change the name of file in getOutputFile method and it would look like below

public class CustomReport  extends TestListenerAdapter implements IReporter {

     @Override
        public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
                                   String outputDirectory) {

        }
   ...
   //paste the content of TestHTMLReporter.java here
   ...
   ...

Make sure all your imports are in place from TestHTMLReporter.java Now, in this file change as per your requirement . For ex: if you'd like to add the end time of each of the test then at the correct place in generateTable method add the below snippet

// Test class
      String testClass = tr.getTestClass().getName();
       long testMillis = tr.getEndMillis();
        String testMillisString = Long.toString(testMillis);
      if (testClass != null) {
        pw.append("<br>").append("Test class Name: ").append(testClass);

         // this line to add end time in ms
        pw.append("<br>").append("End Time(ms): ").append(testMillisString); 
        // Test name
        String testName = tr.getTestName();
        if (testName != null) {
          pw.append(" (").append(testName).append(")");

        }   

Then you'll get like below

Now, You'll get two reports one with default and the other with your file name. The only thing now remains is switching off the default reporting listeners, so you get only one report. For that you can follow this or you may search for solutions. Hope this helps

这篇关于Dataprovider 如何将测试数据发布到 HTML 报告中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆