2016-06-10 55 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() { 
    } 
} 
相關問題