在org.scalatest.FunSuite上定义超时

时间:2018-05-18 14:16:39

标签: scala scalatest

JUnit允许“失控”或耗时太长的测试自动失败(https://github.com/junit-team/junit4/wiki/timeout-for-tests)。例如:

@Test(timeout=1000)
public void testWithTimeout() {
  ...
}

如何使用org.scalatest.FunSuite定义时间输出?

1 个答案:

答案 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)
    }
  }
}

因此会失败,因此会失败。