invocationCount Attribute in @Test Annotation.

How to use "invocationCount", "invocationTimeOut" and "skipfailedInvocations" Attributes?

invocationCount Attribute

It refers to the number of times a method should be invoked.

In below example, iteration count is mentioned as 3, hence single method will execute 3 time.

Java Code-

public class Testing_InvocationCount {
@Test(invocationCount= 3)
public void testNG1()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}	
@Test
public void testNG2()
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}

Output-

[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
Method name is :testNG1
Method name is :testNG1
Method name is :testNG2
PASSED: testNG1
PASSED: testNG1
PASSED: testNG1
PASSED: testNG2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================


Related topic-
















invocationTimeOut- Attribute

invocationTimeOut is the maximum number of milliseconds that the total number of invocations on this test method should take.

For Example, if invocationTimeOut is set 30 seconds and invocationCount is set to 10 times, then this method should complete all 10 invocations within 30 seconds; otherwise, the test will be marked as a fail.

This attribute will be ignored if the invocationCount is not specified on this method

Java Code-

public class Testing_invocationTimeOut {
@Test(invocationCount = 3,invocationTimeOut=1)
public void testNG1() throws InterruptedException
{
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
Method name is :testNG1
Method name is :testNG1
Method name is :testNG1
FAILED: testNG1
org.testng.internal.thread.ThreadTimeoutException: Method org.Testngpro.Testing_invocationTimeOut.testNG1() didn't finish within the time-out 1
	.
.
===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================


Related topic-
















skipfailedInvocations Attribute

This attribute can have only boolean value. If the value is set as true and invocationCount is specified with a value > 1, then all invocations after a failure will be marked as a SKIP instead of a FAIL.

This method will be ignored if invocationTimeOut is specified in the @Test annotation.

In normal sceanrio, if we don’t use skipfailedInvocations attribute then it will show fail count as per the iteration mentioned in invocation count.

Java Code-

public class Testing_skipFailedInvocations {	
@Test(invocationCount = 7,skipFailedInvocations=true)
public void testNG1() throws InterruptedException
{
fail("This method is failed");
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Method name is :"+methodName);
}
}

Outcome-

[RemoteTestNG] detected TestNG version 7.4.0
FAILED: testNG1
java.lang.AssertionError: This method is failed
===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

No comments:

Post a Comment