TestNG中的当前InvocationCount

Mub*_*bin 4 eclipse testng

我有一个使用TestNG进行测试的方法,我用下面的注释标记了它:

@Test(invocationCount=10, threadPoolSize=5)
Run Code Online (Sandbox Code Playgroud)

现在,在我的测试方法中,我想获得正在执行的当前invocationCount.那可能吗?如果是,那么我很高兴知道如何.

更恰当的例子:

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

作为参考,我在Eclipse中使用TestNG插件.

Apr*_*ndi 7

您可以通过在测试方法中添加ITestContext参数来使用TestNG依赖项注入功能.请参阅http://testng.org/doc/documentation-main.html#native-dependency-injection.

从ITestContext参数中,您可以调用其返回ITestNGMethod数组的getAllTestMethods ().它应该只返回一个元素的数组,它引用当前/实际的测试方法.最后,您可以调用ITestNGMethod的getCurrentInvocationCount().

您的测试代码应该更像下面的示例,

@Test(invocationCount=10, threadPoolSize=5)
public void testMe(ITestContext testContext) {
   int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
   System.out.println("Executing count: " + currentCount);
}
Run Code Online (Sandbox Code Playgroud)


vkr*_*ams 2

您可以获取当前的调用计数,如下所述

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)

  }
 }
Run Code Online (Sandbox Code Playgroud)

我知道这是一种愚蠢的方式。然而它会达到你的目的。您可以参考 testNG 源类来获取当前的实际调用计数