concept ScalaCheck in category scala

appears as: ScalaCheck
Scala in Action

This is an excerpt from Manning's book Scala in Action.

I’ll start by introducing automated testing and how developers use it in real-world projects. There are two kinds of automated tests: ones you write (the most common) and ones you generate for your code. First I discuss how you can generate tests for your code using the ScalaCheck tool, because it’s easy. Scala, being a statically typed language, enjoys a unique position where tools like ScalaCheck can generate tests for your functions or classes based on types. ScalaCheck is a great way to get started with automated tests. But to truly get the benefit of automated tests, you also have to write them manually.

10.2.1. Testing the behavior of a string with ScalaCheck

To create a new property in ScalaCheck, you have to use the org.scalacheck.Prop trait. The property in ScalaCheck is represented by instances of the Prop trait. There are several ways to create an instance of a property in ScalaCheck but the one you’re going to use here is org.scalacheck.Prop.forAll.

forAll is a factory method that creates a property that can be tested by ScalaCheck. This method takes a function as an argument that should return a Boolean and can take any type of parameter as long as there’s a generator. Generators are components used by ScalaCheck to generate test data. (You’ll read more about generators later in this section.) Here’s how the property would look for the statement I made about reverse in the previous section:

Prop.forAll((a: String) => a.reverse.reverse == a)

The way to read the preceding property is this: for all strings, the expression (a: String) => a.reverse.reverse == a should hold true (this matches with the claim in the previous section). ScalaCheck will use the generator for String type to generate random string data to validate the statement. To run this property with SBT, you need to wrap it inside the Properties class (later I show you how to use ScalaCheck with your tests). The org.scalacheck.Properties represents a collection of ScalaCheck properties, and SBT has built-in support for running Properties:

package checks
import org.scalacheck._

object StringSpecification extends Properties("String") {
  property("reverse of reverse gives you same string back") =
    Prop.forAll((a: String) => a.reverse.reverse == a)
}

Save the preceding code in a StringSpecification.scala file under the src/test/scala folder of your project, and run the test action from the SBT prompt. If the setup is correct so far, you’ll notice that ScalaCheck has tried 100 times to falsify your property but failed (see figure 10.1).

Figure 10.1. The output from running ScalaCheck from the SBT prompt

By defining the generator as an implicit val, you don’t have to pass it to ScalaCheck properties—ScalaCheck will pick it up. The generator you’ve defined here only generates Int values, but if you wanted to play with different types of values, you’d also define the generator like this:

implicit def arbitraryEither[X, Y](implicit xa: Arbitrary[X],
            ya: Arbitrary[Y]): Arbitrary[Either[X, Y]] =
    Arbitrary[Either[X, Y]](
      oneOf(arbitrary[X].map(Left(_)), arbitrary[Y].map(Right(_)))
    )

The generators for both Left and Right are type-parameterized so they’ll take any type of parameter for which the arbitrary generator is defined in ScalaCheck.

The previous listing creates a generator for the Either type by using the building blocks provided by ScalaCheck. Arbitrary.arbitrary is one of those building blocks that lets you create new custom generators. Using it, you create generators for both Left and Right values of the Either type. Then, using the combinators available in the Gen object, you create a generator for the Either type. The rest of the code is defining ScalaCheck properties for all the specifications declared at the beginning of the section.

There are plenty of Scala open source libraries, like Scalaz (https://github.com/scalaz/scalaz) and Lift (https://github.com/lift/framework), that use ScalaCheck for testing their classes. You can always download them and go through their ScalaCheck tests to see various ways you can use them.

It’s also easy to use ScalaCheck to test Java codebases. Because Scala and Java interoperate, you don’t have to do anything special to test java codebases. ScalaCheck also supports the generation of Java collection classes.

As you’ve already figured out, ScalaCheck is a powerful framework. For example, with 20–25 lines of code, you managed to generate 600 tests. With the ability to create custom generators, I’m sure you can think of places in your project where ScalaCheck will be valuable.