从测试用例设置 TestNG 超时

2024-03-22

我见过很多类似的例子:

@Test(timeOut = 1000)

是否可以在测试用例中覆盖超时值?如果是这样,怎么办?

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


这是可能的,但这是一个非常棘手的技巧,我不推荐。

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 https://github.com/cbeust/testng/issues/1061

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从测试用例设置 TestNG 超时 的相关文章

随机推荐