I'm trying to implement a simple property check but Scalacheck is ignoring my generators. What I'm doing wrong here?
object AlgorithmTest extends Properties("Algorithm") {
property("Test") = forAll (Gen.choose(0,10)) (n => n>=0 & n<10)
}
and this is the result in SBT
[info] ! Algorithm.Test: Falsified after 12 passed tests. [info] >
ARG_0: -1 [error] Failed: : Total 1, Failed 1, Errors 0, Passed 0,
Skipped 0
It looks like the Shrink instance which is passed to the forAll
method is not using the generator when searching for smaller counter-examples. If you change your property to:
property("Test") = Prop.forAllNoShrink(Gen.choose(1, 10)) (n => n >= 0 && n < 10)
Then it should properly fail with:
[info] ! Algorithm.Test: Falsified after 7 passed tests.
[info] > ARG_0: 10
[error] Failed: : Total 1, Failed 1, Errors 0, Passed 0, Skipped 0
One way to visualize the Shrink values is to use the Prop.collect
method:
property("Test") = Prop.forAll(Gen.choose(1, 10)) { n =>
Prop.collect(n) { n >= 0 && n < 10 }
}
Then the collected values look like:
[info] ! Algorithm.Test: Falsified after 40 passed tests.
[info] > ARG_0: -1
[info] > Collected test data:
[info] 17% 3
[info] 17% 1
[info] 15% 6
[info] 12% 9
[info] 10% 2
[info] 10% 5
[info] 7% 4
[info] 7% 8
[info] 2% -1
[info] 2% 7
Where you can see that -1 has been used during the shrinking process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With