Skip to content

Instantly share code, notes, and snippets.

@dwickern
Created February 16, 2017 18:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dwickern/6ba9c5c505d2325d3737ace059302922 to your computer and use it in GitHub Desktop.
Save dwickern/6ba9c5c505d2325d3737ace059302922 to your computer and use it in GitHub Desktop.
Scalatest: run each test N times using a command-line argument
import org.scalatest._
sealed trait Repeated extends TestSuiteMixin { this: TestSuite =>
protected abstract override def runTest(testName: String, args: Args): Status = {
def run0(times: Int): Status = {
val status = super.runTest(testName, args)
if (times <= 1) status else status.thenRun(run0(times - 1))
}
run0(args.configMap.getWithDefault("times", "1").toInt)
}
}
// sample usage:
class SomeTest extends FunSuite with Matchers with Repeated {
test("sometimes fails") {
scala.util.Random.nextDouble() shouldBe > (0.05d) // 5% chance to fail
}
}
/*
$ sbt "test-only SomeTest -- -Dtimes=20"
[info] SomeTest:
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails
[info] - sometimes fails *** FAILED ***
[info] 0.012614120654887317 was not greater than 0.05 (Repeated.scala:19)
[info] - sometimes fails
[info] - sometimes fails *** FAILED ***
[info] 0.022310937247545737 was not greater than 0.05 (Repeated.scala:19)
[info] - sometimes fails
[info] - sometimes fails
[info] Run completed in 365 milliseconds.
[info] Total number of tests run: 20
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 18, failed 2, canceled 0, ignored 0, pending 0
[info] *** 2 TESTS FAILED ***
[error] Failed tests:
[error] SomeTest
[error] (test:testOnly) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 1 s, completed Feb 16, 2017 10:28:48 AM
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment