ScalaCheck Cookbook v1.0

download ScalaCheck Cookbook v1.0

of 38

  • date post

    10-May-2015
  • Category

    Technology

  • view

    4.865
  • download

    2

Embed Size (px)

description

This document is a brief introduction to ScalaCheck and will introduce readers to the basic concepts of ScalaCheck, including code examples (available from github here: It was developed within Accenture and open sourced under the Apache 2.0 License.

Transcript of ScalaCheck Cookbook v1.0

  • 1.Accenture Architecture Innovation ScalaCheck CookbookAccenture Architecture InnovationScalaCheck CookbookLast Modified: 5/15/2012 11:38 AM Page 1 of 38 Copyright 2012 Accenture.

2. Accenture Architecture Innovation ScalaCheck CookbookContentsAccenture Architecture Innovation ...................................................................................................................... 1ScalaCheck Cookbook .......................................................................................................................................... 11 Introduction ................................................................................................................................................... 41.1 Overview........................................................................................................................................................................ 41.2 Knowledge Requirements ............................................................................................................................................. 42 What is ScalaCheck? ....................................................................................................................................... 42.1 Benefits of ScalaCheck compared to traditional unit testing ........................................................................................ 42.2 Use Cases ....................................................................................................................................................................... 43 Getting Started ............................................................................................................................................... 53.1 Properties and Generators ............................................................................................................................................ 53.2 Creating tests with ScalaCheck ...................................................................................................................................... 53.3 Running the sample snippets ........................................................................................................................................ 64 Working with Properties ................................................................................................................................. 64.1 Defining a simple property ............................................................................................................................................ 64.2 Grouping properties ...................................................................................................................................................... 74.3 Advanced Property Usage ............................................................................................................................................. 74.3.1 Conditional properties ....................................................................................................................................... 74.3.2 Combining properties ........................................................................................................................................ 85 Generators ..................................................................................................................................................... 85.1 Built-in generators ......................................................................................................................................................... 95.2 Custom generators ........................................................................................................................................................ 95.3 The Gen companion object ......................................................................................................................................... 115.4 The Arbitrary generator ............................................................................................................................................... 115.5 More generators.......................................................................................................................................................... 126 Analyzing test data ....................................................................................................................................... 127 ScalaCheck Integration ................................................................................................................................. 147.1 Using ScalaCheck to test Java code ............................................................................................................................. 147.1.1 The scenario ..................................................................................................................................................... 157.1.2 The tests ........................................................................................................................................................... 167.2 Using ScalaCheck with Scala Specs .............................................................................................................................. 187.3 Using ScalaCheck with ScalaTest ................................................................................................................................. 207.4 Using ScalaCheck with JUnit ........................................................................................................................................ 237.4.1 Using JUnits assertTrue ................................................................................................................................... 237.4.2 Using a custom JUnit runner ............................................................................................................................ 248 Real World Scenario: Testing a Hadoop MAp-Reduce Job with ScalaCheck.................................................... 258.1 Hadoop ........................................................................................................................................................................ 258.2 The scenario ................................................................................................................................................................ 258.2.1 WordCount mapper ......................................................................................................................................... 268.2.2 WordCount reducer ......................................................................................................................................... 268.2.3 Input and output .............................................................................................................................................. 278.2.4 Other requirements ......................................................................................................................................... 278.3 Defining the test cases ................................................................................................................................................ 278.4 Building the generators ............................................................................................................................................... 278.4.1 Building the basic generators........................................................................................................................... 288.4.2 Hadoop generators .......................................................................................................................................... 298.4.3 Testing the Generators .................................................................................................................................... 308.5 Building the property checks ....................................................................................................................................... 318.5.1 Some implicits to make our life easier ............................................................................................................. 318.5.2 Handling of single words by the mapper ......................................................................................................... 318.5.3 Mapping longer lines........................................................................................................................................ 328.5.4 Reducing data .................................................................................................................................................. 33Last Modified: 5/15/2012 11:38 AMPage 2 of 38 Copyright 2012 Accenture. 3. Accenture Architecture Innovation ScalaCheck Cookbook8.6 Putting it all together .................................................................................................................................................. 349 Appendix 1: References ................................................................................................................................ 359.1 Sample code ................................................................................................................................................................ 359.2 Links ............................................................................................................................................................................. 3610 Appendix 2: DEPLOYING SCALACHECK .......................................................................................................... 3610.1 Using JAR files .............................................................................................................................................................. 3610.2 Maven .............................................................