I'm trying to use ScalaTest with ScalaCheck to do property based testing. I've got the tests outlined below:
import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}
object Calc {
def add(a:Int, b:Int) = a+b
def div(a:Int, b:Int) = a/b
}
class PropSpec1 extends FlatSpec with Matchers with PropertyChecks {
behavior of Calc.getClass.getName
it should "add integers" in {
forAll { (a: Int, b: Int) =>
Calc.add(a, b) shouldEqual a + b
}
}
it should "divide integers" in {
forAll {
(a:Int, b:Int) => Calc.div(a, b) shouldEqual a/b
}
}
}
Now what I'm seeing is that if I keep running the tests in PropSpec1 over and over, sometimes the second test passes, but most of the time it fails. Now, if 0 isn't tested for b, then obviously it'll pass, but I'd have thought that's one of the things it would always try. I see the same behaviour when running sbt clean test repeatedly; sometimes both tests pass.
Is this normal for property based testing, or is there something I need to do (like always providing my own generator)?