How to get the current invocation count in TestNG?

TestNGRest AssuredDynamic Programming

TestNG supports multi-invocation of a test method, i.e., a @Test method can be invoked multiple times sequentially or in parallel. If we want to run single @Test 10 times at a single thread, then invocationCount can be used.

To invoke a method multiple times, the keyword invocationCount = <int> is required. For example −

@Test(invocationCount = 10)

In this example, the @Test method will execute for 10 times each on a single thread.

In this article, we will illustrate how to get the current invocation count.

Approach/Algorithm to solve this problem

  • Step 1 − Create a TestNG class, NewTestngClass.

  • Step 2 − Write two @Test methods in the class NewTestngClass as shown in the programming code section below. Add invocationCount=10

  • Step 3 − Create the testNG.xml as given below to run the TestNG classes.

  • Step 4 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

  • Step 5 − In the output, the user can see a total of 1 thread running sequentially for all invocations of @Test.

Example

Use the following code for the commonTestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.annotations.*;
public class NewTestngClass {
   @Test(invocationCount = 10)
   public void testcase1(ITestContext testContext){
      System.out.println("Running testcase 1");
      int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @Test(invocationCount = 10)
   public void testcase2(ITestContext testContext){
      System.out.println("Running testcase 2");
      int currentCount = testContext.getAllTestMethods()[1].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

Running testcase 1
Executing count: 0
Running testcase 1
Executing count: 1
Running testcase 1
Executing count: 2
Running testcase 1
Executing count: 3
Running testcase 1
Executing count: 4
Running testcase 1
Executing count: 5
Running testcase 1
Executing count: 6
Running testcase 1
Executing count: 7
Running testcase 1
Executing count: 8
Running testcase 1
Executing count: 9
Running testcase 2
Executing count: 0
Running testcase 2
Executing count: 1
Running testcase 2
Executing count: 2
Running testcase 2
Executing count: 3
Running testcase 2
Executing count: 4
Running testcase 2
Executing count: 5
Running testcase 2
Executing count: 6
Running testcase 2
Executing count: 7
Running testcase 2
Executing count: 8
Running testcase 2
Executing count: 9
===============================================
Suite1
Total tests run: 20, Passes: 20, Failures: 0, Skips: 0
==================================================
raja
Published on 12-Jan-2022 12:20:09

Advertisements