Current InvocationCount in TestNG

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

@Test(invocationCount=10, threadPoolSize=5) 

Now, in my testing method, I would like to get the current invocationCount that is executing. Is it possible? If so, then I would be glad to know how.

More correct 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 the TestNG plugin in Eclipse.

+4
source share
4 answers

You can use the TestNG dependency injection function by adding the ITestContext parameter to your test method. See http://testng.org/doc/documentation-main.html#native-dependency-injection .

From the ITestContext parameter, you can call it getAllTestMethods () , which returns an ITestNGMethod array. It should return an array of only one element that refers to the current / actual testing method. Finally, you can call getCurrentInvocationCount () from the ITestNGMethod method.

Your test code should be more or less similar to 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); } 
+4
source

You can get it by calling getCurrentInvocationCount() ITestNGMethod

+1
source

You can use something like this:

 public class getCurrentInvocationCount { AtomicInteger i = new AtomicInteger(0); @Test(invocationCount = 10, threadPoolSize=5) public void testMe() { int count= i.addAndGet(1); System.out.println("Current Invocation count "+count) } } 
+1
source

You can get a current call score as follows

 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 some kind of stupid way. However, it will serve your purpose. You can reference the source class testNG to get the actual current invocationCount

0
source

Source: https://habr.com/ru/post/1486522/


All Articles