Define @Test With
"TimeOut":
@Test(timeOut = 5000): The
test method creditCardPayment will fail
if it takes more than 5 seconds to execute.
package TestNgBasics1;
import org.testng.annotations.Test;
public class TestMethodWithTimeOut {
// define test method with timeout
= 5 sec
@Test(timeOut =
5000)
// 5 sec
if test method takes > 5 sec to execute , it fails test method
public void
creditCardPayment() throws InterruptedException
// credit card payments must be
done in 5 sec
{
System.out.println("
test with time out =5 sec");
Thread.sleep(8000);
System.out.println("This
method should Take max 5 sec");
//FAILED:
creditCardPayment
// org.testng.internal.thread.ThreadTimeoutException:
Method TestNgBasics.TestAnontwithTimeout1.
//creditCardPayment()
didn't finish within the time-out 5000
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
test with time out =5 sec
FAILED: creditCardPayment
org.testng.internal.thread.ThreadTimeoutException: Method TestNGBasics1.TestMethodWithTimeOut.creditCardPayment()
didn't finish within the time-out 5000
at
java.base@15.0.2/java.lang.invoke.MemberName.<init>(MemberName.java:822)
at
java.base@15.0.2/java.lang.invoke.MethodHandleNatives.varHandleOperationLinkerMethod(MethodHandleNatives.java:560)
at
java.base@15.0.2/java.lang.invoke.MethodHandleNatives.linkMethodImpl(MethodHandleNatives.java:475)
at
java.base@15.0.2/java.lang.invoke.MethodHandleNatives.linkMethod(MethodHandleNatives.java:463)
at
java.base@15.0.2/java.util.concurrent.FutureTask.setException(FutureTask.java:247)
at
java.base@15.0.2/java.util.concurrent.FutureTask.run(FutureTask.java:269)
at
java.base@15.0.2/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base@15.0.2/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at
java.base@15.0.2/java.lang.Thread.run(Thread.java:832)
===============================================
Default test
Tests run: 2, Failures:
1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
EX2: Valid Example (+ve)
package TestNGBasics1;
import org.testng.annotations.Test;
public class TestMethodWithTimeOut
{
// define test method with
timeout = 5 sec
// @Test(timeOut =
5000)
// 5 sec
if test method takes > 5 sec to execute , it fails test method
@Test(timeOut =
9000) // if test method takes < 9 sec
for execution, it will pass Test method
public void
creditCardPayment() throws InterruptedException
// credit card payments must be
done in 5 sec
{
System.out.println("
test with time out =5 sec");
Thread.sleep(6000);
System.out.println("This
method should Take max 5 sec");
//FAILED:
creditCardPayment
// org.testng.internal.thread.ThreadTimeoutException:
Method TestNgBasics.TestAnontwithTimeout1.
//creditCardPayment()
didn't finish within the time-out 5000
}
}
o/p:
[RemoteTestNG] detected TestNG version 7.0.0
test with time ou =5 sec
This method should Take max 5 sec
PASSED: creditCardPayment
===============================================
Default test
Tests run: 1, Failures:
0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
No comments:
Post a Comment