2016-06-09 46 views
1

我见过很多类似这样的例子:设置TestNG的超时从测试用例

@Test(timeOut = 1000) 

是否有可能从测试用例中覆盖的超时值?如果是这样,怎么样?

我的问题是,测试用例的执行时间是由传递给测试用例的参数控制的。有时候测试用例需要一个小时,有时需要很多天。我想相应地设置超时时间。

回答

2

这是可能的,但它是非常棘手的黑客,我不建议。

public class DynamicTimeOutSample { 

    private final long timeout; 
    private final long waitTime; 

    @DataProvider 
    public static Object[][] dp() { 
    return new Object[][]{ 
     new Object[]{ 1_000, 2_000 }, 
     new Object[]{ 3_000, 6_000 }, 
    }; 
    } 

    @Factory(dataProvider = "dp") 
    public DynamicTimeOutSample(long timeout, long waitTime) { 
    this.timeout = timeout; 
    this.waitTime = waitTime; 
    } 

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

    @Test 
    public void test() throws InterruptedException { 
    Thread.sleep(waitTime); 
    } 
} 

该示例的两次测试用例将按预期失败。

我创建的功能要求,但不要指望很快就会明白:https://github.com/cbeust/testng/issues/1061

+1

的'IAnnotationTransformer'可以工作了。 –

+0

@CedricBeust是的,但只有当你想要工厂的每个实例都有相同的超时时间时,不是吗? – juherr

+0

啊对了。 ........ –