Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current InvocationCount in TestNG

Tags:

eclipse

testng

I have a method to be tested using TestNG and I have marked it with below annotations:

@Test(invocationCount=10, threadPoolSize=5)

Now, in my test method I would like to get the current invocationCount that is being executed. Is that possible? If yes, then I would be glad to know how.

More proper example:

@Test(invocationCount=10, threadPoolSize=5)
public void testMe() {
   System.out.println("Executing count: "+INVOCATIONCOUNT); //INVOCATIONCOUNT is what I am looking for
}

For reference, I am using TestNG plugin in Eclipse.

like image 611
Mubin Avatar asked Jun 17 '13 01:06

Mubin


2 Answers

You can use TestNG dependency injection feature by adding ITestContext parameter in your test method. Please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection.

From the ITestContext parameter, you can call its getAllTestMethods() which returns array of ITestNGMethod. It should returns array of only one element, which refers to the current/actual test method. Finally, you can call getCurrentInvocationCount() of ITestNGMethod.

Your test code should be more-less like the following sample,

@Test(invocationCount=10, threadPoolSize=5)
public void testMe(ITestContext testContext) {
   int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
   System.out.println("Executing count: " + currentCount);
}
like image 113
Aprian Diaz Novandi Avatar answered Oct 24 '22 00:10

Aprian Diaz Novandi


You can get the current invocation count as mentioned below

public class getCurrentInvocationCount {
int count;

 @BeforeClass
 public void initialize() {
     count = 0;
  }

 @Test(invocationCount = 10)
 public void testMe()  {
   count++;
   System.out.println("Current Invocation count "+count)

  }
 }

I know this is a some kind of stupid way. However it will server your purpose. You can refer testNG source class to get actual current invocationCount

like image 32
vkrams Avatar answered Oct 23 '22 23:10

vkrams