且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使ScalaCheck测试具有确定性

更新时间:2023-02-06 19:15:13

如果您使用的是纯ScalaCheck属性,则应该能够使用Test.Params类更改所使用的java.util.Random实例并提供您的自己的,总是返回相同的一组值:

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

[已更新]

我刚刚发布了一个新的specs2-1.12.2-SNAPSHOT,您可以在其中使用以下语法来指定您的随机生成器:

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)