How to run a test twice using TestNG

testng run test multiple times with different parameters
how to run one test case multiple times in testng
how to run multiple testng tests in parallel
testng run suite multiple times
testng invocationcount
how to run selected test cases in testng
which testng listener being used to execute failed test cases again?
how to run test cases sequentially in testng

I am doing automation tests using TestNG and Maven. I need to run a single test twice, first running on one parameter (for example state=1) and later and the same parameter but with a another value (state=2).

I have a lot (more than 50) methods with @Test annotation. The idea is to make every test to be invoked w times, writing as less code as possible.

How to make my test run twice with this 2 states?

Here's one way of doing this. Please feel free to expand this solution to whatever is your actual problem statement.

  1. Make sure you are using TestNG 7.0.0-beta1 (latest released version as of today)
  2. Within your suite xml file, add a suite level parameter that captures all the state values for which you need the tests to be repeated separating them by ,
  3. Build a org.testng.IAlterSuiteListener implementation that basically splits the suite level parameter into multiple parameters, retries the tests in the suite, clones them and adds the split parameters to each of the tests.

The below sample should show how this would work.

Test class looks like this

package com.rationaleemotions.stackoverflow.qn53803675;

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SampleTestClass {

  public void testMethodOne(int state) {

  public void testMethodTwo(int state) {

  private void printer(int state) {
    ITestResult result = Reporter.getCurrentTestResult();
    String methodname = result.getMethod().getMethodName();
    String testname = result.getTestContext().getName();
    String msg =
        String.format("%s() from <%s> running with state [%d]", methodname, testname, state);

Here's how the suite altering listener would look like

package com.rationaleemotions.stackoverflow.qn53803675;

import java.util.ArrayList;
import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class SimpleSuiteAlterer implements IAlterSuiteListener {

  public void alter(List<XmlSuite> suites) {
    XmlSuite suite = suites.get(0);
    // Fetch the suite level parameter called "states" and split it by comma
    // We will get all the states that we need to run for.
    String[] states = suite.getParameter("states").split(",");
    // We are going to assume that only those <test> tags that begin with "dynamic"
    // in their names will be considered for multiple execution.
    List<XmlTest> dynamictests =
            .filter(xmlTest -> xmlTest.getName().startsWith("dynamic"))

    List<XmlTest> clonedTests = new ArrayList<>();
    for (XmlTest each : dynamictests) {
      for (int i = 1; i < states.length; i++) {
        XmlTest cloned = new XmlTest(suite);
        cloned.addParameter("state", states[i]);
        cloned.setName(each.getName() + "_cloned");
      each.addParameter("state", states[0]);

Here's how the suite xml file looks like

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "">
<suite name="53803675_suite" parallel="false" verbose="2">
    <listener class-name="com.rationaleemotions.stackoverflow.qn53803675.SimpleSuiteAlterer"/>
  <parameter name="states" value="1,2"/>
  <test name="dynamic-53803675-test">
      <class name="com.rationaleemotions.stackoverflow.qn53803675.SampleTestClass"/>

Here's how the output looks like

... TestNG 7.0.0-beta1 by Cédric Beust (
testMethodOne() from <dynamic-53803675-test> running with state [1]
testMethodTwo() from <dynamic-53803675-test> running with state [1]
testMethodOne() from <dynamic-53803675-test_cloned> running with state [2]
PASSED: testMethodOne(1)
PASSED: testMethodTwo(1)

    Tests run: 2, Failures: 0, Skips: 0

testMethodTwo() from <dynamic-53803675-test_cloned> running with state [2]
PASSED: testMethodOne(2)
PASSED: testMethodTwo(2)

    Tests run: 2, Failures: 0, Skips: 0

Total tests run: 4, Passes: 4, Failures: 0, Skips: 0

Process finished with exit code 0

TestNG: How can I run same test case multiple times?, How do you run the same test parallel in TestNG? 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.

try like this

//This method will provide data to any test method that declares that its Data Provider
//is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
  { "Cedric", new Integer(36) },
  { "Anne", new Integer(37)},

 //This test method declares that its data should be supplied by the Data Provider
 //named "test1"
 @Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);

see here

How To Run Same Test Multiple Times In Selenium Using TestNG , How do you rerun only fail test cases in TestNG? In TestNG, there’s a concept known as data driven testing, which allows testers to automatically run a test case multiple times, with different input and validation values. A good example of this is, suppose, you have to test thousands of forms using automation.

Use invocationCount - TESTNG

    @Test(invocationCount = 2)
    public void test() {

The test above will be executed 2 times.

You can set invocationCount global if it is applicable for all the tests e.g.

public class InvocationCount implements IAnnotationTransformer {

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

Parallel Execution of test methods in TestNG, So do i need to right the same test case twice in a flow? Or tell me some solution to handle these kind of scenarios. Flow is: Act 1: UI text  TestNG parallel execution of tests, classes and suites with examples. Learn how to run testng tests and suites in parallel or single test in multiple threads. Parallelism or multi-threading in software terms is defined as the ability of the software, operating system, or program to execute multiple parts or sub-components of another program simultaneously.

I prefer answer provided by Krishnan Mahadevan but if you think need to avoid coding then can use two <Test> in testng.xml file.

<test name="Test1" preserve-order="true">
    <parameter name="State" value="State1"></parameter>


        <class  name="" />

</test> <!-- Test -->

 <test name="Test2" preserve-order="true">
    <parameter name="State" value="State2"></parameter>


        <class  name="" />

</test> <!-- Test -->

So Test1 runs with one state and Test2 runs with another state.

use @Test(invocationCount = 2) to repeat the same test

Question: How to prevent tests to be run twice when having suites , Question: How to prevent tests to be run twice when having suites eriwen added a:question from:contributor in:testing labels on Jul 27, 2017. TestNG Test Suite Attribute. The testng.xml file has support for several attributes which you can use while running a test. We have used some of these in the examples shown after this list: name: Name of Test Suite; verbose: You can change the test execution log details in console by specifying verbose level. Possible value is between 1-10.

How to execute same @Test method multiple times with different , How can I execute same @test method twice, but with different I would like to be able to specify my testng.xml something like below. How can  I would like to be able to specify my testng.xml something like below. How can I achieve this? I need the tests to be run on same instance, so I must put them within same <test> tag.

TestNG executes @BeforeClass annotated method twice in , When we execute our Test case directly using the Eclipse TestNG plug- in all is well and the @BeforeClass method gets executed once. We see this "expected"  TestNG - Writing Tests. Write the business logic of your test and insert TestNG annotations in your code. Add the information about your test (e.g. the class name, the groups you wish to run, etc.) in a testng.xml file or in build.xml. Run TestNG.

There is test run duplication when using a TestNG suite XML file with , using a TestNG suite XML file and the maxParallelForks option where if you have a second TestNG test class the tests get executed twice. I specified both of them in testng.xml file and when running using maven, I am seeing that all the methods of 1st specified class in testng are running and it hangs….class 2 methods are not running. its hanging Can you please help me what am i doing wrong below is my testng xml Below is output of execution [INFO] Scanning for projects…

  • Can you show code of your test?
  • Sorry, no. Please explain how to run the test Twice, because maven I am using clean-install and run-excel via Maven.
  • use data provider
  • Can you give an example?
  • Thanks for your answer. so, now I need to add @Parameters("state") to each Test That I wish to run twice? (in addition to the other changes)
  • Yes. You need to add @Parameters annotation to your tests and add the needed parameters as well to your test method. Please accept my answer if it helped you with your question.
  • Your code look nice, but did you tried it? I keep getting java.lang.NumberFormatException: For input string: "1,2"
  • The output I shared in the post is the output from my own code. So not sure what you mean by "did you try it"
  • I am running tests via Test Suites, I don't run a single test XML. What should I do?
  • I have many tests in my file. How to make each one of them to run twice?
  • @TalAngel Is these two states same to all your tests, i means say state1 -> running all test cases, state2->running all test cases. ? or they differ test case to test case?