2016-06-10 71 views
1

我被困在框架中的一个点上。如何在运行时在TestNG中设置invocationCount值@Test Annotation

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

因此,我所做的是:

@Test(invocationCount=3) 

这对我来说完美的工作。但我的问题是我想用一个变量来设置这个参数的值。

E.g.我有一个变量&我想是这样的:

int x=5; 

@Test(invocationCount=x) 

是否有任何可能的方式来做到这一点或执行相同的@Test注解的次数任何其他好办法。

在此先感谢。

回答

1

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

你有2种选择:

如果x是恒定的,你可以使用一个IAnnotationTransformer

否则,您可以使用黑客喜欢:

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() { 
    } 
} 
相关问题