Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

testng - How Dataprovider publish the test data into HTML report

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...