@Test annotation attributes – invocationCount attribute

Sometimes we encounter questions like how to run a single test case multiple times? Well, invocationCount is the answer to that question. In this post, we will discuss the invocationCount attribute used with @Test annotation in TestNG.

What does this attribute do, or what is the use of invocation count? invocationCount helps in executing a single test case multiple times. So, if you have a test case that needs to be run multiple times, invocationCount can help you.

Syntax

@Test(invocationCount = num) where num = the number of times you want to run this test method.

Example
package Test;

import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test(invocationCount = 5) // now, this test case will run 5 times
	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Below is the XML file to run the above test class

<suite name="codekru">
	<test name="codekruTest">
		<classes>
			<class name="Test.CodekruTest">
			</class>
		</classes>
	</test>
</suite>

Output after running the above XML file-

test2 is passed
test2 is passed
test2 is passed
test2 is passed
test2 is passed
PASSED: test2
PASSED: test2
PASSED: test2
PASSED: test2
PASSED: test2

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

So, here we can see that test() ran five times.

The What If scenarios

What if we kept invocationCount =0? What will happen then?

Here the test case won’t even run, as shown in the below example

package Test;

import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test(invocationCount = 0) // now, this test case will run 5 times
	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Output after running the same XML file –

===============================================
codekru
Total tests run: 0, Passes: 0, Failures: 0, Skips: 0
===============================================

Here we can see that no test case or method was executed.

What if we kept invocationCount as a negative value?

It won’t give any error, but the test case won’t be executed.

package Test;

import org.testng.Assert;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test(invocationCount = -2) // now, this test case will run 5 times
	public void test2() {

		System.out.println("test2 is passed");
		Assert.assertTrue(true);
	}

}

Output –

===============================================
codekru
Total tests run: 0, Passes: 0, Failures: 0, Skips: 0
===============================================

We hope you liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Other @Test attributes

Liked the article? Share this on

Leave a Comment

Your email address will not be published.