Search code examples
scalaspecs2scalacheck

Make ScalaCheck tests deterministic


I would like to make my ScalaCheck property tests in my specs2 test suite deterministic, temporarily, to ease debugging. Right now, different values could be generated each time I re-run the test suite, which makes debugging frustrating, because you don't know if a change in observed behaviour is caused by your code changes, or just by different data being generated.

How can I do this? Is there an official way to set the random seed used by ScalaCheck?

I'm using sbt to run the test suite.

Bonus question: Is there an official way to print out the random seed used by ScalaCheck, so that you can reproduce even a non-deterministic test run?


Solution

  • If you're using pure ScalaCheck properties, you should be able to use the Test.Params class to change the java.util.Random instance which is used and provide your own which always return the same set of values:

    def check(params: Test.Parameters, p: Prop): Test.Result

    [updated]

    I just published a new specs2-1.12.2-SNAPSHOT where you can use the following syntax to specify your random generator:

    case class MyRandomGenerator() extends java.util.Random {
      // implement a deterministic generator 
    }
    
    "this is a specific property" ! prop { (a: Int, b: Int) =>
      (a + b) must_== (b + a)
    }.set(MyRandomGenerator(), minTestsOk -> 200, workers -> 3)