I'm using Scalacheck and want to generate collection of a given size. There is a special function for that in scalaCheck, Gen.listOfN(size, Gen[T])
.
When in forAll
method I print the size of the generated collection it does not always have the defined size. Actually it only has the given size on the first attempt. For example, size 6 --> first attempt the size of collection is 6, second attempt size is only 3. What am I doing wrong?
1 Answers
It sounds like you might be using an old (pre-1.11.0) version of ScalaCheck. In these versions, the generator boundaries weren't always respected.
When ScalaCheck finds a failing test case for your property, it tries to simplify that test case (make it "smaller"). Nowadays (version >= 1.11.0), ScalaCheck tries to respect for example listOfN
when doing this simplification, and not test lists with fewer than n
items. However, in some cases it is still not possible for ScalaCheck to know what boundaries a generator had from the start, for example when you use the Gen.map
method.
For more information on the cases when ScalaCheck still might simplify test cases in unexpected ways (and what you can do to mitigate it), see: Scalacheck won't properly report the failing case