user3782636
user3782636

Reputation: 83

How to make testng listeners runs before the @AfterClass annotation?

Scenario - I have a listener which generates the report for the execution. I want to add the generated test report to database. I have a function in @AfterClass annotation which needs to add the generated report to database.

Problem I'm facing - Listener is generating the report only @AfterClass method gets executed.

Is there a way to make the listener generate the reports once the @Test annotation tests get executed and add the generated report to database in @AfterClass annotation?

Please let me know if there is any better way to achieve this.

Upvotes: 1

Views: 854

Answers (1)

Vault23
Vault23

Reputation: 753

If you implemented your own test listener with TestListenerAdapter, you have methods onStart, onFinish, onTestSuccess and onTestFailure. So if you need to generate your report after each test, just put your logic inside these methods.

public class TestListener extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult iTestResult) {
    //your logic for report generation
    }

    @Override
    public void onFinish(ITestContext iTestContext) {
    //your logic for report generation
    }

    @Override
    public void onTestSuccess(ITestResult iTestResult) {
    //your logic for report generation
    }
}

I don't know in what cases you should generate (and how) your report, but in accordance with your needs you can put your report logic in relevant methods.

Upvotes: 0

Related Questions