There is a special generator, org.scalacheck.Arbitrary.arbitrary
, which generates arbitrary values of any
supported type.
Generators are responsible for generating test data in ScalaCheck, and are represented by the org.scalacheck.Gen
class.
Generators are responsible for generating test data in ScalaCheck, and are represented by the org.scalacheck.Gen
class. In the Gen
object, there are several methods for creating new and modifying existing generators.
We will show how to use some of them in this section. For a more complete reference of what is available,
please see the API scaladoc.
A generator can be seen simply as a function that takes some generation parameters, and (maybe) returns a
generated value. That is, the type Gen[T]
may be thought of as a function of type Gen.Params => Option[T]
.
However, the Gen
class contains additional methods to make it possible to map generators, use them in
for-comprehensions and so on. Conceptually, though, you should think of generators simply as functions, and the
combinators in the Gen
object can be used to create or modify the behaviour of such generator functions.
A property is the testable unit in ScalaCheck, and is represented by the org.scalacheck.Prop
class.
A property is the testable unit in ScalaCheck, and is represented by the org.scalacheck.Prop
class.
There are several ways to create properties in ScalaCheck, one of them is to use the org.scalacheck.Prop.forAll
method like in the example below.
scala> val propConcatLists = forAll { (l1: List[Int], l2: List[Int]) => l1.size + l2.size == (l1 ::: l2).size }
We can use the check
method to test it:
scala> propConcatLists.check
+ OK, passed 100 tests.
OK, that seemed alright. Now, we'll define another property.
scala> val propSqrt = forAll { (n: Int) => scala.math.sqrt(n*n) == n }
And check it:
scala> propSqrt.check ! Falsified after 2 passed tests. > ARG_0: -1 > ARG_0_ORIGINAL: -488187735
Not surprisingly, the property doesn't hold. The argument -1
falsifies it. You can also see that the argument
-488187735
falsifies the property. That was the first argument ScalaCheck found, and it was then
simplified to
-1
.
The forAll
method creates universally quantified properties directly, but it is also
possible to create new properties by combining other properties, or to use any of the specialised
methods in the org.scalacheck.Prop
object.
scalacheck-datetime is a library for helping use datetime libraries with ScalaCheck
scalacheck-datetime is a library for helping use datetime libraries with ScalaCheck
The motivation behind this library is to provide a simple, easy way to provide generated date and time instances that are useful to your own domain.
For SBT, you can add the dependency to your project’s build file:
resolvers += Resolver.sonatypeRepo("releases") "com.fortysevendeg" %% "scalacheck-datetime" % "0.2.0" % "test"
Please, visit the homepage for more information
ScalaCheck is a tool for testing Scala and Java programs, based on property specifications and automatic test data generation.
The
arbitrary
GeneratorThere is a special generator,
org.scalacheck.Arbitrary.arbitrary
, which generates arbitrary values of any supported type.The
arbitrary
generator is the generator used by ScalaCheck when it generates values for property parameters. Most of the times, you have to supply the type of the value toarbitrary
, like above, since Scala often can't infer the type automatically. You can usearbitrary
for any type that has an implicitArbitrary
instance. As mentioned earlier, ScalaCheck has default support for common types, but it is also possible to define your own implicitArbitrary
instances for unsupported types. See the following implicit Arbitrary definition for booleans, that comes from the ScalaCheck implementation.