David Portabella
David Portabella

Reputation: 12710

define timeout on org.scalatest.FunSuite

JUnit allows tests that 'runaway' or take too long, to be automatically failed (https://github.com/junit-team/junit4/wiki/timeout-for-tests). Example:

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

How can I define a timeput using org.scalatest.FunSuite?

Upvotes: 2

Views: 468

Answers (1)

Xavier Guihot
Xavier Guihot

Reputation: 61646

You can use failAfter from the Timeouts trait:

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

which will thus fail, because it timeouts.

Upvotes: 2

Related Questions