Nikhil Indurkar
Nikhil Indurkar

Reputation: 31

Close was called before test could end safely using EndTest when running the test in multiple threads

I am getting below ExtentTestInterruptedException exception when I am running my test in multiple threads(Using TestNG to launch test in multiple threads):

com.relevantcodes.extentreports.ExtentTestInterruptedException: 
Close was called before test could end safely using EndTest.
    at com.relevantcodes.extentreports.Report.terminate(Report.java:425)
    at com.relevantcodes.extentreports.ExtentReports.close(ExtentReports.java:917)

Steps Followed

  1. I am starting my test in @BeforeTest method with extent report startTest method
  2. And ending my test with extent report endTest in @AfterMethod method
  3. My extent report close method is defined in @AfterSuite function.
  4. I removed close() method call from @AfterSuite but in that case result for few of the test cases appears as unknown.

Can someone please help me in resolving this query?

Upvotes: 3

Views: 3127

Answers (2)

Siva
Siva

Reputation: 13

When test fails, if it is not handled for the extend.endTest() you will face this issue. I have used the below ITestListener method to handle the test failure. Here the reason is captured and logged to the report and end the extent report logging.

public void onTestFailure(ITestResult t)
    {
        if(t.getStatus()==ITestResult.FAILURE) {
            String reason = t.getThrowable().toString();
            logger.log(LogStatus.FAIL, reason);
            extent.endTest(logger);
        }

    }

Upvotes: 0

Devdun
Devdun

Reputation: 767

This is related to extent report. I'm not sure how you write your @after method but only what i can guess is you use reports.endTest(test); in your code. This is in older version of extent report. Most probably your problem is with it. Here is an alternative with latest extent report solution since no extent report related code for your problem is available in here.

@AfterMethod
public synchronized void afterMethod(ITestResult result) {

    StringBuilder inputArgs = new StringBuilder();
    Object objects[] = result.getParameters();

    for(Object obj : objects){
        if(obj==null){
            inputArgs.append("  ");
        }else{
            inputArgs.append(obj.toString());
        }
        inputArgs.append(" , ");
    }

    if (result.getStatus() == ITestResult.FAILURE)
        test.get().fail(result.getThrowable()+ "Input Parameters : "+inputArgs.toString());
    else if (result.getStatus() == ITestResult.SKIP)
        test.get().skip(result.getThrowable() + "Input Parameters : "+inputArgs.toString());
    else
        test.get().pass( " Test Passed. Input parameters : " +inputArgs.toString());
    extent.flush();
}

Upvotes: 1

Related Questions