Chapter 6. ScalaCheck

Scala Check is automated testing. To be more precise, it fully automates test generation so that there is no need to create test data. We have already seen two ways to feed test data into ScalaTest. TestNG covered the TestNG DataProvider that lets the end user create test data and send it to the test method for processing. Specs2 has DataTable functionality that allows the developer to create an ASCII-like table with test data that is similarly thrown into the test line-by-line. ScalaCheck is fundamentally different from these frameworks; it generates semirandom data within the parameters you request, so you don’t have to take the time to come up with test data. Not only does it randomly generate data, saving time, it also makes your code more robust, because a human tester is not likely to think of the full range of values that the program can receive during real use.

ScalaCheck is derived from the Haskell product QuickCheck, and is open source.

There are three main components to ScalaCheck. One is a Properties class that defines tests and runs them through a test harness called Prop. Properties can be mixed and matched with various groupings and combinations, as well as filtered to provide only the data needed for the test.

ScalaCheck also provides a Gen object, which is a generator class that provides much of the fake data and allows you to control the kind of data created. For instance, if you want only positive integers, you can use Gen to eliminate negative ...

Get Testing in Scala now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.