While using scalacheck if we test any property with forAll then we only get reported failed test inputs and none passed test inputs.
scala> import org.scalacheck.Prop.forAll
scala> val propConcatLists = forAll { (l1: List[Int], l2: List[Int]) =>
l1.size + l2.size == (l1 ::: l2).size }
scala> propConcatLists.check
+ OK, passed 100 tests.
Is there any way to report all the random test inputs given by forAll to respective property test?
Use collect
.
This toy example illustrates its use:
import org.scalacheck.Prop.{forAll,collect}
val q = forAll { (m: Int, n: Int) => collect(m, n, m+n) { m + n != 37 } }
q.check
The above code yields this output
+ OK, passed 100 tests. > Collected test data: 2% (0,-1,-1) 2% (-1,0,-1) 2% (-1,2147483647,2147483646) <snip> 1% (1,0,1) 1% (-1199549050,-1564573392,1530844854) 1% (2147483647,0,2147483647) 1% (-1,-2147483648,2147483647)
Disclaimer: This toy example is clearly not a well designed property test. While it is definitely not true that an arbitrary pair of Ints
never sums to 37, it passes ScalaCheck
because the chances of two arbitrary Ints
generated by ScalaCheck
summing to 37 is pretty darn small. But if 37 were changed to any of -2, -1, 0, 1, or 2, the test would probably fail because the values -1, 0, and 1 are generated disproportionately frequently by Scalacheck's
implicit arbitrary[Int]
generator.