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

How to accept Junit 5 test after timeout?

当我将 @timeout 置于测试上方时,当运行时间过长时它会失败。

是否要将其标记为已通过?

根据@timeout 的文档,如果该方法不能在规定的时间内完成其操作,则@timeout 将导致测试失败。因此,如果该方法花费的时间超过预期,则不能使用 @timeout 将其标记为已通过。

否则,根据another question,为了达到你的目的,你可以使用JavaThread和sleep()方法来检查Thread.isAlive()是否在一段时间后. 示例:

...

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

...