Appu Mistri
Appu Mistri

Reputation: 853

How to generate TestNg output results before running @AfterSuite annotation

In my @AfterSuite annotation, I am trying to generate Jasper report that takes the testng-results.xml as input. But, the issue here is, the testng-results.xml gets generated only after executing @AfterSuite annotation. I would like to know if it is possible to generate the test results before running @AfterSuite annotation. Any help/suggestion is appreciated.

I know there are some answers related to this question here. But i did not found an exact way to do it.

Upvotes: 1

Views: 469

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

The file testng-results.xml is generated by the TestNG's built-in reporter org.testng.reporters.XMLReporter. This reporter is kicked off only at the reporting phase (i.e., only after all the suites have run to completion).

So there's no way that this file can be generated before the @AfterSuite annotated method runs to completion.

You could instead build your logic of constructing your jasper based reports via the listener org.testng.IExecutionListener in its onExecutionFinish() method.

This listener would get called after the reporting phase wherein all the reports have been generated.

The other option is you do the following:

  1. Create a new reporter that extends org.testng.reporters.XMLReporter
  2. Configure TestNG to not run the default reports. For e.g., if you were using Maven as your build tool and were using surefire-plugin, then you could configure surefire-plugin to disable the default reports by adding the following
<properties>
    <property>
        <name>usedefaultlisteners</name>
        <value>false</value>
    </property>
</properties>
  1. Wire in the listener you created in (1) via :
    1. @Listeners annotation (or)
    2. <listeners> tag (or)
    3. Using service loaders [ See here ]

Upvotes: 3

Related Questions