Checking property

The check() method accepts Test.Parameters, which allow for the configuration of a few aspects of how the check is executed. The most useful describe a minimum number of successful tests, the number of workers to run in parallel, a test callback to execute after each test, the maximum discard ratio between passed and discarded tests for conditional tests, and an initial seed which can help to make the property evaluation deterministic. It is also possible to limit the time the test is allowed to execute. Here is an example, which uses both test parameters and a time limit:

scala> val prop = forAll { a: String => a.nonEmpty ==> (a.reverse.reverse == a) }
prop: org.scalacheck.Prop = Prop

scala> val timed = within(10000)(prop)
timed: org.scalacheck.Prop = Prop

scala> Test.check(timed) {
| _.withMinSuccessfulTests(100000).withWorkers(4).withMaxDiscardRatio(3)
| }
res47: org.scalacheck.Test.Result = Result(Failed(List(),Set(Timeout)),0,0,Map(),10011)

Here we used the Test.check method, which executes a property with given parameters and returns test statistics back. We can see that our test has failed because of the timeout.

Besides within, there are other wrapper methods defined on Prop. For instance, it is possible to convert exceptions thrown by the property into test failures, to evaluate properties lazily, or to collect data for the test report:

scala> forAll { a: String =>
| classify(a.isEmpty, "empty string", "non-empty string") {
| a.sorted.length ?= a.length
| }
| }.check()
+ OK, passed 100 tests.
> Collected test data:
96% non-empty string
4% empty string

The difference between == and ?= used in the previous code is subtle—the == compares two values and returns a Boolean, which is then implicitly converted to the Prop; the ?= creates a Prop directly and sometimes it can be useful in the situations where properties are combined, as we'll see further.

A property can also be labelled, which makes it easier to spot in the results:

scala> val prop2 = "Division by zero" |: protect(forAll((a: Int) => a / a == 1))
prop2: org.scalacheck.Prop = Prop

scala> prop2.check()
! Exception raised on property evaluation.
> Labels of failing property:
Division by zero
> ARG_0: 0
> Exception: java.lang.ArithmeticException: / by zero
$line74.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.$anonfun$prop2$2(<console>:2
3)
...

Here we also used the protect method to convert the exception into the test failure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175