使ScalaCheck测试具有确定性 [英] Make ScalaCheck tests deterministic
问题描述
我想暂时使我的specs2测试套件中的ScalaCheck属性测试具有确定性,以简化调试.现在,每次我重新运行测试套件时,可能会生成不同的值,这使调试令人沮丧,因为您不知道观察到的行为变化是由代码更改还是仅由生成的不同数据引起的.
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.
我该怎么做?是否有官方方法可以设置ScalaCheck使用的随机种子?
How can I do this? Is there an official way to set the random seed used by ScalaCheck?
我正在使用sbt
运行测试套件.
I'm using sbt
to run the test suite.
奖励问题:是否有官方方法可以打印 ScalaCheck使用的随机种子,以便甚至可以重现非确定性的测试运行?>
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?
推荐答案
如果您使用的是纯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)
这篇关于使ScalaCheck测试具有确定性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!