How to reduce the number of test cases generated by ScalaCheck?

I am trying to solve two problems of ScalaCheck (+ specs2):

  • Is there a way to change the number of cases that ScalaCheck generates?

  • How can I generate strings containing some Unicode characters?

For example, I would like to create about 10 random strings that include both alphanumeric and Unicode characters. However, this code always generates 100 random strings, and they are strictly alpha characters:

"make a random string" in {
    def stringGenerator = Gen.alphaStr.suchThat(_.length < 40)
    implicit def randomString: Arbitrary[String] = Arbitrary(stringGenerator)

    "the string" ! prop { (s: String) => (s.length > 20 && s.length < 40) ==> { println(s); success; } }.setArbitrary(randomString)
}

Edit

I just realized that there is another problem:

  1. Often ScalaCheck refuses, without generating 100 test cases.

Of course, I do not want 100, but, apparently, my code is trying to create too complex a set of rules. The last time he ran, I saw that "surrendered after 47 tests."

+4
1

" 47 " , ( suchThat ==>) . , , - ( , -):

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

, .

maxDiscardRatio:

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

: , , .

, :

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

, , .

+8

Source: https://habr.com/ru/post/1589132/


All Articles