简体   繁体   中英

How to define JUnit test time-out at runtime (i.e. without annotations)?

I want to run a unit test with a time-out that is defined at runtime. I want to define a time-out for a specific test only, not the whole class.

I saw these are the ways to set the time out:

@Rule
public Timeout globalTimeout = new Timeout(10000); // 10 seconds max per method tested

or

@Test(timeout=100) public void infinity() {
   while(true);
}

but when I run this code, no exception is thrown. I want to identify when the test failed due to timeout.

public Timeout testTimeout;

private void setTestTimeOut() {
    if (!Strings.isNullOrEmpty(testTimeOut)) {
        testTimeout = new Timeout(Integer.parseInt(testTimeOut));
    }
}

How can I catch the exception? Wrap the main method with try-catch(InterruptException) ?

Add a TestWatcher rule that decides, at run time, whether to apply a timeout or not:

@Rule
public TestWatcher watcher = new TestWatcher() {
  @Override
  public Statement apply(Statement base, Description description) {
    // You can replace this hard-coded test name and delay with something
    // more dynamic
    if (description.getMethodName().equals("infinity")) {
      return new FailOnTimeout(base, 200);
    }

    return base;
  }
};

Your original approach didn't work because JUnit rules take effect before the test code begins to run, so any attempts to adjust your Timeout object within your test are too late.

The @Test(timeout=xxx) will not throw a TimeoutException , it will fail the test.

Could you specify in more detail what you would like to test?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM