How to execute the same test method multiple times in TestNG

This post will discuss how to execute the same test method or test case multiple times. Let’s first create a test method that we can run multiple times.

Below is our CodekruTest class which will have one test method that we will run five times.

CodekruTest.java

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

public class CodekruTest {

	@Test
	public void testMethod() {
		System.out.println("Executing testMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

Now, we will be using the invocationCount attribute of the @Test annotation to execute a single test case multiple times. Let’s try to run the test case five times; then, we will use invocationCount = 5.

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

public class CodekruTest {

	@Test(invocationCount = 5)
	public void testMethod() {
		System.out.println("Executing testMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

Below is our XML file to execute the CodekruTest test class

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

Output after running the XML file

Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class

So, here we can see that the testMethod executed five times. If you want to know more about the invocationCount attribute, we suggest you read this article. Please note that the above test method wasn’t executed parallelly. Instead, the test case was executed in a sequence, which means that the five executions ran one after the other.

Running a single test case multiple times and that too in parallel

To run a single test case numerous times and that too in parallel, we can use an additional attribute of @Test annotation, threadPoolSize. Below is the sample code where we used both invocationCount and the threadPoolSize attribute.

public class CodekruTest {

	@Test(invocationCount = 10, threadPoolSize = 5)
	public void testMethod() {
		System.out.println("Executing testMethod in CodekruTest class");
		Assert.assertTrue(true);
	}

}

Now, after running the same XML file, five test case instances will run in parallel.

Output after running the XML file

Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class
Executing testMethod in CodekruTest class

We hope that you liked this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *