ScalaCheck began as a straightforward port of QuickCheck, a highly automated and powerful testing tool for the Haskell programming language. I had previously used QuickCheck with great pleasure when developing software in Haskell, and ScalaCheck was born from a desire to transfer the advanced testing technology found in that tool into the Java and Scala world. At that time, I had recently begun programming in Scala and found it advanced and rich enough to make ScalaCheck equally as useful and as elegant as QuickCheck.
By now, ScalaCheck has been extended with features that are not found in Haskell's QuickCheck, but it still shares the same core concept: the idea of property-based testing. This is the concept that separates ScalaCheck and QuickCheck from the class of mainstream testing tools, which among many others includes the JUnit framework. You will find that property-based testing requires a slightly different way of thinking about tests and specifications, and hopefully you will discover a valuable complement to your ordinary set of testing tools. In short, property-based testing allows you to write properties that describe your code's behavior, and leave the task of test case generation and property evaluation to the testing tool. This book aims to introduce and promote the idea of property-based testing and to be a comprehensive guide to ScalaCheck itself.
ScalaCheck is an open-source tool, released under a license that makes it free to use in both commercial and non-commercial projects. ScalaCheck is well established in the Scala community and is used in several prominent Scala projects. Examples include the Scala compiler itself, the Akka concurrency framework and the library for functional programming in Scala, scalaz.
This book targets Java and Scala programmers who want to explore an alternative way of unit testing. The book does not introduce the Scala programming language; however, it uses only basic Scala code. If you have no previous experience with Scala, you should be able to pick it up along the way, while reading some introductory Scala material on the side. A great thing about Scala and ScalaCheck is the complete interoperability with Java, which allows you to test your existing Java programs using the novel techniques found in Scala and ScalaCheck.
Readers are expected to have previous experience with JUnit or similar testing frameworks.
If you have previous Scala experience, you know that you can write Scala programs in a functional style as well as a more Java-like imperative style. In a similar vein, you could look at ScalaCheck as a more functional or declarative testing tool than JUnit. This is not far-fetched since the raw model QuickCheck is implemented in the functional programming language Haskell. Therefore, readers intrigued by Scala's functional aspects might find themselves interested in ScalaCheck.
If we overlook the Java testing tools, which are also usable for Scala, there are three major testing tools in the Scala ecosystem: ScalaCheck, specs2, and ScalaTest. All three have slightly different focuses and feature sets.
ScalaCheck focuses on property-based testing with automated test-case generation. A great deal of work was put into being able to define a wide range of properties, and making the property definitions as concise as possible. ScalaCheck includes basic functionality for running tests programmatically or from the command line, but does not include a graphical test runner.
specs2 concentrates on a behavior-driven development (BDD) way of testing. You write test cases in specs2 in a very descriptive form, much like traditional specifications in plain English. You can also use ScalaCheck properties in specs2, thereby taking advantage of ScalaCheck's automatic test-case generation.
ScalaTest is a flexible testing platform focused on enabling productivity of teams through simple, clear tests and executable specifications. ScalaTest enables teams to use the style of testing that fits them best, and supports property-based testing via integration with ScalaCheck.
As you can see, ScalaCheck is compatible with both specs2 and ScalaTest. Therefore, you can leverage ScalaCheck's unique abilities on any testing framework you choose. ScalaCheck intends to provide a property-based testing engine that complements other testing tools. It is not necessarily a replacement for other tools. Maybe you find that ScalaCheck properties are a great idea but that some tests just seem better to write in a traditional style; in that case, you can choose to use ScalaTest or specs2 in combination with ScalaCheck. Of course, you can always go all-in on ScalaCheck, since it comes complete with a basic test runner that you can integrate into most build processes without much effort. There's also built-in support for ScalaCheck in the build tool sbt, as described in Chapter 7.
The examples presented in this book are written purely for ScalaCheck, but the concepts and methods presented are usable in both specs2 and ScalaTest, where ScalaCheck provides the property-based aspects of the framework.
This eBook is not simply a printable version of the book. While the content is the same as the print version, the eBook has been carefully prepared for reading on a computer screen.
The first thing to notice is that most references within the book are hyperlinked. If you select a reference to a chapter, or figure, your browser or reader should take you immediately to the selected item so that you do not have to flip around to find it.
Additionally, at the bottom of each page are several navigation links. The Cover, Overview, and Contents links take you to major portions of the book. The Index link takes you to the reference part of the book. Finally, the Discuss link takes you to an online forum where you can discuss questions with other readers, the author, and the larger Scala community. If you find a typo, or something you think could be explained better, please click on the Suggest link, which will take you to an online web application where you can give the author feedback.
Although the same pages appear in the eBook as the printed book, blank pages are removed and the remaining pages renumbered. The pages are numbered differently so that it is easier for you to determine PDF page numbers when printing only a portion of the eBook. The pages in the eBook are, therefore, numbered exactly as your PDF reader will number them.
The first time a term is used, it is italicized. Small code examples, such as x + 1, are written inline with a mono-spaced font. Larger code examples are put into mono-spaced quotation blocks like this:
def hello() { println("Hello, world!") }
When interactive shells are shown, responses from the shell are shown in a lighter font.
scala> 3 + 4 res0: Int = 7
18.188.211.106