gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:43:28 24 4
gpt4 key购买 nike

我的框架一度卡住了。

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

所以我所做的是:

@Test(invocationCount=3)

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

例如我有一个变量,我想要的是这样的:

int x=5;

@Test(invocationCount=x)

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

提前致谢。

最佳答案

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() {
}
}

关于java - 如何在运行时在 TestNG @Test Annotation 中设置 invocationCount 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37741194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com