从testcase设置TestNG超时 [英] Set TestNG timeout from testcase

查看:236
本文介绍了从testcase设置TestNG超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多类似的例子:

I've seen lots of examples similar to this:

@Test(timeOut = 1000)

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

Is it possible to override the timeout value from within the testcase? If so, how?

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

My problem is that the execution time of the testcase is controlled by an argument that is passed to the testcase. Sometimes the testcase takes one hour, sometimes many days. I want to set the timeout accordingly.

推荐答案

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

It is possible but it is very a tricky hack that I don't recommend.

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);
  }
}

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

The twice test cases of the example will fail as expected.

我创建了功能请求,但不要指望它很快就会出现: https://github.com/cbeust/testng/issues/1061

I created the feature request, but don't expect to have it soon: https://github.com/cbeust/testng/issues/1061

这篇关于从testcase设置TestNG超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆