简体   繁体   English

超时后如何接受Junit 5测试?

[英]How to accept Junit 5 test after timeout?

When I place @timeout above the test then it fails when runs too long.当我将@timeout放在测试上方时,它会在运行时间过长时失败。

Hot to mark it as passed instead?热到将其标记为已通过?

Based on the documentation of @timeout, @timeout is made to fail the test if the method doesn't meet to finish its operation within the required time.根据@timeout 的文档,如果方法没有满足在规定时间内完成其操作,则@timeout 测试失败。 Hence, you cannot use @timeout to mark it as passed if the method is taking longer than it's expected.因此,如果方法花费的时间比预期的要长,则不能使用 @timeout 将其标记为已通过。

Otherwise, based on another question , to achieve your purpose, you can use Java Thread and sleep() method to check whether the Thread.isAlive() after some period of time.否则,基于另一个问题,为了达到您的目的,您可以使用 Java Thread 和 sleep() 方法在一段时间后检查 Thread.isAlive() 是否。 Example:例子:

...

// Create and start the task thread.
Thread taskThread = new Thread(){
    public void run(){
      System.out.println("Thread Running");
    }
  }
taskThread.start( );

// Wait 3 seconds.
sleep(3000);
boolean status = false;

// If after waiting 3 seconds the task is still running, stop it.
if (taskThread.isAlive( )) {
    taskThread.interrupt( );
} else {
    status = true;
}

assertTrue(status);
    

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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