JUnit允许“失控”或耗时太长的测试自动失败(https://github.com/junit-team/junit4/wiki/timeout-for-tests)。例如:
@Test(timeout=1000)
public void testWithTimeout() {
...
}
如何使用org.scalatest.FunSuite
定义时间输出?
答案 0 :(得分:2)
您可以使用Timeouts
特征中的failAfter
:
import org.scalatest.concurrent.Timeouts
import org.scalatest.time.{Span, Millis}
import org.scalatest.FlatSpec
class MyTest extends FlatSpec with Timeouts {
"This test" should "do smhtg" in {
// Here the timeout is 100ms
failAfter(Span(100, Millis)) {
// fails due to timeout:
Thread.sleep(200)
// or succeeds before timeout:
// assert(true)
// or fails before timeout:
// assert(1 === 2)
}
}
}
因此会失败,因此会失败。