Can we call @AfterMethod before ITestListener

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Can we call @AfterMethod before ITestListener

garvitagirotraev
Hi,

I am having multiple @Test in 1 class. In each @Test, there are many soft assertions. So i have put SoftAssert.assertAll() in @AfterMethod.

But as Listener is called before @AfterMethod, it prints status of test as PASS. Is there a way to call @AfterMethod before ITestListener. I tried with onConfigurationSuccess with IConfigureListener, but did not work out , i have ,many configuration methods.

Here is my Listener Class:

public class TestListener implements ITestListener {
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_RESET = "\u001B[0m";
   
    @Override
    public void onTestSuccess(ITestResult result) {
        getTestLog(result, "PASS", ANSI_GREEN);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        getTestLog(result, "FAIL", ANSI_RED);
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        getTestLog(result, "SKIP", ANSI_YELLOW);
    }

    
    private void getTestLog (ITestResult result, String testStatus, String statusColor) {
        float timetakenToExecute = (result.getEndMillis() - result
                .getStartMillis()) / 1000.0f;
        StringBuilder log = new StringBuilder();
        String methodname = result.getTestClass().getName() + "." + result
                .getMethod().getMethodName();
        String status = statusColor + testStatus + ANSI_RESET;
        log.append(methodname);
        log.append(" ...  ");
        log.append(status);
        log.append(" ");
        log.append("(" + timetakenToExecute + "s)");
        System.out.println(log);
    }
}

Here is my test class :

public class MyClass
{

  @Test(priority = 1)
    public void chart1() {
          util.createChart("param1");   // All assertions are implemented in Util class. Once chart is created, all assertions are called.
    }

    @Test(priority = 2)
    public void chart2() {
           util.createChart("param2");
    }

@AfterMethod
    public void assertAll() {
         util.getSoftassert().assertAll();
    }


Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can we call @AfterMethod before ITestListener

akash kansal
Try this one .

public class SoftAssertListener implements IInvokedMethodListener {

    private static final ThreadLocal<SoftAssertions> THREAD_LOCAL_CONTAINER_FOR_SOFT_ASSERTIONS = new ThreadLocal<>();

    @Override
    public void beforeInvocation(final IInvokedMethod method, final ITestResult testResult) {
        if (method.isTestMethod()) {
            THREAD_LOCAL_CONTAINER_FOR_SOFT_ASSERTIONS.set(new SoftAssertions());
        }
    }

    @Override
    public void afterInvocation(final IInvokedMethod method, final ITestResult testResult) {
        if (method.isTestMethod() && testResult.getStatus() == SUCCESS) {
            try {
                getSoftAssert().assertAll();
            } catch (AssertionError e) {
                testResult.getTestContext().getPassedTests().removeResult(testResult.getMethod());
                testResult.setStatus(TestResult.FAILURE);
                testResult.setThrowable(e);
            }
            THREAD_LOCAL_CONTAINER_FOR_SOFT_ASSERTIONS.remove();
        }
    }

    public static SoftAssertions getSoftAssert() {
        return THREAD_LOCAL_CONTAINER_FOR_SOFT_ASSERTIONS.get();
    }
}


Add this class and use it in your @Test

On Thu, Jul 19, 2018 at 3:29 PM, <[hidden email]> wrote:
Hi,

I am having multiple @Test in 1 class. In each @Test, there are many soft assertions. So i have put SoftAssert.assertAll() in @AfterMethod.

But as Listener is called before @AfterMethod, it prints status of test as PASS. Is there a way to call @AfterMethod before ITestListener. I tried with onConfigurationSuccess with IConfigureListener, but did not work out , i have ,many configuration methods.

Here is my Listener Class:

public class TestListener implements ITestListener {
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_RESET = "\u001B[0m";
   
    @Override
    public void onTestSuccess(ITestResult result) {
        getTestLog(result, "PASS", ANSI_GREEN);
    }

    @Override
    public void onTestFailure(ITestResult result) {
        getTestLog(result, "FAIL", ANSI_RED);
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        getTestLog(result, "SKIP", ANSI_YELLOW);
    }

    
    private void getTestLog (ITestResult result, String testStatus, String statusColor) {
        float timetakenToExecute = (result.getEndMillis() - result
                .getStartMillis()) / 1000.0f;
        StringBuilder log = new StringBuilder();
        String methodname = result.getTestClass().getName() + "." + result
                .getMethod().getMethodName();
        String status = statusColor + testStatus + ANSI_RESET;
        log.append(methodname);
        log.append(" ...  ");
        log.append(status);
        log.append(" ");
        log.append("(" + timetakenToExecute + "s)");
        System.out.println(log);
    }
}

Here is my test class :

public class MyClass
{

  @Test(priority = 1)
    public void chart1() {
          util.createChart("param1");   // All assertions are implemented in Util class. Once chart is created, all assertions are called.
    }

    @Test(priority = 2)
    public void chart2() {
           util.createChart("param2");
    }

@AfterMethod
    public void assertAll() {
         util.getSoftassert().assertAll();
    }


Thanks !!

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.