How to run same test multiple times in Selenium using TestNG?

In the previous tutorial, we discussed some of the important Git commands used to perform operations on our code repository. Hope you enjoyed reading the article. Well, today we are going to discuss the technique to run same test multiple times in our Selenium project with the help of TestNG. This is one of the TestNG interview questions recently asked my friends, so I thought to write a detailed article over this. It might help some of the needy one.

Before we begin, I have some interesting reading suggestions for you.

These reading suggestions will improve your knowledge bank.

Why do we run same test multiple times?

It is very important to understand the scenario of running same test multiple times. If we have the logical answer to this question then we are good to design our test plan with the right automation feasibility.

With data driven testing, we run same tests multiple times with different test data, but here we are just invoking the same test multiple times and reason could be anything; like-

  • Checking the dynamic changes in UI
  • Validating dynamic table
  • Load testing
  • Performance testing
  • Stability testing of the test framework itself
  • Test execution time calculation

These are the quick reasons to run same test multiple times. If you have some more reasons, then do share with us through the comment section which is at the bottom of this blog post. 😊

How to run same test multiple times using TestNG?

First, create a TestNG class file and add all the required annotations. Identify that @Test annotation which you want to run multiple times. Once you identified the @Test annotation then design the test format as below.

@Test(invocationCount = numberOfCount)

Here we use keyword named as invocationCount with some numeric value. It invokes the @Test multiple times and the invocation count is equal to the integer value assigned to the invocationCount keyword.

Let’s have a look at the sample program below:

Sample Program

package Test;

import org.testng.annotations.Test;

public class MultiTimeRunner {
	
  @Test(invocationCount=5)
  public void testRunner() {
	  System.out.println("Sample Test");
  }
}

In the above sample program, I just created a TestNG class file and added @Test annotation with invocationCount value equals 5. So the testRunner() method written inside the @Test annotation will be executed five times.

Here is the sample test.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Inviul Sample Test Suite">
<listeners>
 <listener class-name="Test.SeleniumPDFReportWithIReporter"/>
 <listener class-name="Test.ListenersDefinitionClass"/>
</listeners>
<!-- Test -->
  <test name="Inviul Test" >
    <classes>
      <class name="Test.MultiTimeRunner"/>
    </classes>
  </test> 
  <!-- End Test -->
  
</suite> <!-- Suite -->

The following screenshot is the console output which shows testRunner() method invoked for five times.

Run same test multiple times console output

This was all about running same test multiple times using TestNG. You can share your views and queries by using our comment section below and Do not forget to join our Facebook group.

Join Inviul fb group

One Response

Leave a Reply