Java 从testcase设置TestNG超时

Java 从testcase设置TestNG超时,java,testng,Java,Testng,我见过很多类似的例子: @Test(timeOut = 1000) 是否可以从testcase中重写超时值?如果是,怎么做 我的问题是testcase的执行时间由传递给testcase的参数控制。有时候测试用例需要一个小时,有时候需要很多天。我想相应地设置超时。这是可能的,但这是一个非常棘手的问题,我不推荐使用 public class DynamicTimeOutSample { private final long timeout; private final long wait

我见过很多类似的例子:

@Test(timeOut = 1000)
是否可以从testcase中重写超时值?如果是,怎么做


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

这是可能的,但这是一个非常棘手的问题,我不推荐使用

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);
  }
}
该示例的两次测试用例将按预期失败


我创建了功能请求,但不希望很快得到它:

@cedricbe必须是,但前提是您希望工厂的每个实例都有相同的超时,不是吗?