Sateesh
Sateesh

Reputation: 1015

testNG: How to capture return value of test case either in @AfterMethod or listener?

just to set the context, my test case creates some data and i want to delete that data in teardown. i would like to implement generic teardown process (through AfterMethod/AfterClass/Listener) in such a way that it will inspect the return value (and its type, mostly domain objects) and cleans up the data(some thing like below).

public class SampleTest {

@AfterMethod
public void teardown(Object returnValueFromTest){
    //inspect returnValueFromTest and perform necessary clean up.
}

@Test
public String testEventGeneration(){
    //generate event

    //returning generated event id.
    return "E1234";
}

@Test
public Market testMarketGeneration(){
    //generate market

    //returning generated market.
    return someMarket;
}}

any ideas/thoughts on how to achieve this in testNG? I considered implementing a listener like IHookable as well but could not find the listener that can help me get hold of return value.

Upvotes: 1

Views: 5227

Answers (3)

moose
moose

Reputation: 11

have you considered using InvokedMethodListener2?

public Class BaseTestNg {

    ITestContext ctx;

    public ITestContext getContext(){
        return this.ctx;
    }

    @BeforeClass
    public void setContext( final ITestContext ctx ) {
        this.ctx = ctx;
    }
}

public Class SampleTest extends BaseTestNg {

    @Test
    public Market testMarketGeneration(){
        //generate market
        this.getContext().setAttribute("market", someMarket);
        //returning generated market.
        return someMarket;
    }
}


import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener2;
import org.testng.ITestContext;
import org.testng.ITestResult;

public class InvokedMethodListener2Impl implements IInvokedMethodListener2 {

    @Override
    public void afterInvocation( final IInvokedMethod method, final ITestResult testResult ) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {

        //read object from context
        //Market = (Market) context.getAttribute("market");

        // testResult.setStatus( ITestResult.FAILURE );

    }

    @Override
    public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult ) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeInvocation( final IInvokedMethod method, final ITestResult testResult, final ITestContext context ) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 0

niharika_neo
niharika_neo

Reputation: 8531

AFAIK Testng wouldn't even execute methods annotated as Test if there is a return value associated with them.

So, if you want to teardown based on values set, then you would need to set those values in a global context or externally and not as return values. You can write your teardown logic either as a listener or in any of the After methods, based on when you want to teardown.

Upvotes: 0

Dharshana
Dharshana

Reputation: 1222

You can simply handle this ,If you want to wind out everything after all tests are executed you have to use @AfterClass annotation instead of @AfterMethod annotation. @AfterMethod annotation triggers after every test method in the test class.

And flowing code will simply do your task . If you want to implement this in listner level you can implement ITestListener and on onFinish method you can implement your logic.

public class SampleTest { private String event_id = null;

@AfterClass(alwaysRun = true)
public void teardown(){
    //delete event from the db(event_id) 

}

@Test
public void testEventGeneration(){
    //generate event

    //returning generated event id.
    event_Id= "E1234";
}}

Upvotes: 1

Related Questions