|
Scalacheck is a test framework which is designed for property based testing. The main difference between a more
traditional unit testing framework and a property based framework is that with a traditional framework, you have
to provide the data with which to test your classes. With a property based framework, it provides the data. You tell
it what sort of data you want, and then it generates a set of data, and runs the tests. You need to provide some
code that asserts that a combination of data is correct. Let's have an example. In chapter 1, we created a specs2
test to test the buyer kitten matching algorithm. It looked like this:
objectLogicSpec extends Specification {
"The 'matchLikelihood' method" should {
"be 100% when all attributes match" in {
val tabby = Kitten("1", List("male", "tabby"))
valprefs = BuyerPreferences(List("male", "tabby"))
Logic.matchLikelihood(tabby, prefs) must beGreaterThan(.999)
}
"be 0% when no attributes match" in {
val tabby = Kitten("1", List("male", "tabby"))
valprefs = BuyerPreferences(List("female", "calico"))
val result = Logic.matchLikelihood(tabby, prefs)
result must beLessThan(.001)
}
}
}
These are good tests, but two test cases to cover the logic in this test seems a bit light. So, to up our confidence in
the algorithm a bit, we can add tests for the same method using Scalacheck:
|
|