如何在TestNG @Test Annotation中的运行时设置invocationCount值

时间:2016-06-10 06:20:48

标签: java annotations testng

我的框架中有一点陷入困境。

我想多次运行@Test注释。为此,我用Google搜索并找到了一个用@Test注释设置invocationCount变量的解决方案。

所以我做的是:

@Test(invocationCount=3)

这对我来说非常合适。但我的问题是我想用变量设置这个参数的值。

E.g。我有一个变量&我想要的是:

int x=5;

@Test(invocationCount=x)

是否有任何可能的方法来执行此操作或任何其他好的方法来执行相同的@Test注释多次。

提前致谢。

1 个答案:

答案 0 :(得分:1)

Set TestNG timeout from testcase是一个类似的问题。

您有两个选择:

如果x不变,您可以使用IAnnotationTransformer

否则,您可以使用hack:

public class DynamicTimeOutSample {

  private final int count;

  @DataProvider
  public static Object[][] dp() {
    return new Object[][]{
        new Object[]{ 10 },
        new Object[]{ 20 },
    };
  }

  @Factory(dataProvider = "dp")
  public DynamicTimeOutSample(int count) {
    this.count = count;
  }

  @BeforeMethod
  public void setUp(ITestContext context) {
    ITestNGMethod currentTestNGMethod = null;
    for (ITestNGMethod testNGMethod : context.getAllTestMethods()) {
      if (testNGMethod.getInstance() == this) {
        currentTestNGMethod = testNGMethod;
        break;
      }
    }
    currentTestNGMethod.setInvocationCount(count);
  }

  @Test
  public void test() {
  }
}
相关问题