简体   繁体   中英

TestNg email reports are not current

I have recently converted my project from JUnit5 to TestNG, solely for the purpose of getting decent reports.

I have added a listener that generates the report at the end of each run:

@Override
public void onFinish(ITestContext context) {
    System.out.println("FINISH. Sending email report.");
    utils.EmailHandler.sendEmail("Finished test", context.toString());
}

My problem is that the reports being sent by email are not from the current run, as desired, but the previous run.

Yet if I open the report in /test-output/custom-report.html using Eclipse IDE it is the correct one!

How do I ensure the emails sent out are current?

I have looked at a couple of similar questions here, but neither are appropriate to me:

Similar questions:

TestNg emailable-report is not updating?

ReportNG HTML report not updating

It finally worked when I moved the call to sendEmail to the end of the GenerateReport method of the listener. That removes all confusion and ensures that that output file is complete before attempting to send it out.

Unless, you 're somehow attaching the old version; from your description, I would say that most likely the file is created AFTER the email is being sent (hence previous version every-time). However, if this theory is correct, it must have emailed an empty file the first time :) Did it?

Idea: insert a couple of minutes delay in your code where the email is being sent. Go check the file as soon as the email leaves the ground, I think it will be the old version (as it hasn't been created yet!)

Have you tried using the @AfterTest annotation? Not sure, but onFinish(ITestContext context) could be lingering somewhere between @AfterMethod and AfterTest causing your email to leave slightly earlier; before fully attached! Not sure though, why you send an email after each test and not after the whole suite has finished [so to use onFinish(ISuite suite) ].

@AfterTest  
public void afterTest(ITestContext context) {

    //improving answer after initial comments
    if(bufferredWriter!=null){
    bufferredWriter.close();
    }       
    else{
    System.out.println("FINISH. Sending email report.");
    utils.EmailHandler.sendEmail("Finished test", context.toString());
    }   
}

Best of luck!

PS. Nevertheless, I would highly recommend to have a look at extentReports . Definitely better reporting than the built-in that comes with TestNG!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM