How can I reduce the number of test cases ScalaCheck generates?

╄→гoц情女王★ 提交于 2019-12-05 19:14:50
Travis Brown

The "gave up after 47 tests" error means that your conditions (which include both the suchThat predicate and the ==> part) are too restrictive. Fortunately it's often not too hard to bake these into your generator, and in your case you can write something like this (which also addresses the issue of picking arbitrary characters, not just alphanumeric ones):

val stringGen: Gen[String] = Gen.chooseNum(21, 40).flatMap { n =>
  Gen.buildableOfN[String, Char](n, arbitrary[Char])
}

Here we pick an arbitrary length in the desired range and then pick that number of arbitrary characters and concatenate them into a string.

You could also increase the maxDiscardRatio parameter:

import org.specs2.scalacheck.Parameters
implicit val params: Parameters = Parameters(maxDiscardRatio = 1024)

But that's typically not a good idea—if you're throwing away most of your generated values your tests will take longer, and refactoring your generator is generally a lot cleaner and faster.

You can also decrease the number of test cases by setting the appropriate parameter:

implicit val params: Parameters = Parameters(minTestsOk = 10)

But again, unless you have a good reason to do this, I'd suggest trusting the defaults.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!