Reputation: 12710
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
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