Thursday, May 15, 2014

How to abort a test case at runtime if it is taking more time than expectaion or how to use 'timeout' parameter in TestNG.

Note- The 'timeout' means if test case is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed.
'timeout' can also be used for performance testing, to ensure the method is returned within a reasonable time.

ex-

import org.testng.annotations.Test;

public class TimeOut {
    @Test(timeOut=3000) //time is in milliseconds, here this test case will execute for max 3sec if it will not complete in 3sec then it will be failed.
    public void thisTestShouldPass() throws InterruptedException{
        System.out.println("This test case should pass");
        Thread.sleep(1000); //wait for 1 sec
    }
   
    @Test(timeOut=5000)  //here this test case will execute for max 5sec if it will not complete in 5sec then it will be failed.
    public void thisTestShouldFail(){
        System.out.println("This test case should fail");
        while(true); //this loop is never going to end as condition is always true
    }
}

No comments:

Post a Comment