Tools for test-driven development in Scala

Two core Scala libraries support features for mocking and data generation.

Scala, a language designed for well-structured and readable programs, is richly provisioned with testing frameworks. The community has adopted test-driven development (TDD) and behavior-driven development (BDD) with zeal. These represent the baseline for trustworthy code development today.

TDD and BDD expand beyond the traditional model of incorporating a test phase into the development process. Most programmers know that ad hoc debugging is not sufficient and that they need to run tests on isolated functions (unit testing) to make sure that a change doesn’t break anything (regression testing). But testing libraries available for Scala, in supporting TDD and BDD, encourage developers to write tests before they even write the code being tested.

Tests can be expressed in human-readable text reminiscent of natural language (although you can’t stretch the comparison too far) so that you are documenting what you want your code to do while expressing the test that ensures that code ultimately will meet your requirements.

Daniel Hinojosa, author of Testing in Scala, describes the frameworks and their use for testing, TDD, and BDD in this interview.

Highlights from our discussion include:

  • The special advantages of test frameworks for Scala. [Discussed at the 0:10 mark]
  • The two main testing frameworks, ScalaTest and Specs2. It’s worth studying both of these frameworks, but you’ll probably ultimately stick to one based on programming style and how you want to do mocking. [Discussed at the 2:12 mark]
  • Mocking simply means removing operations that will take a long time or require outside support, such as a database. When testing, you want to fool your code into believing that the operation took place while actually simulating it. This is especially critical for TDD, because tests are so extensive and run so regularly. [Discussed at the 04:01 mark]
  • How the new ScalaMock library extends the abilities to mock parts of the system. This is an emerging technology. [Discussed at the 7:36 mark]
  • Generating random input test data. You can actually make your code more robust by throwing garbage values at it rather than by planning what data to input, because a programmer usually fails to anticipate some of the data that will be encountered in production use. For instance, you might not realize how large the input data will be, or might forget to include negative numbers. Scala gives you a full range of control ranging from specifying precise values to allowing completely random input. [Discussed at the 8:24 mark]
  • Looking toward the future of Scala testing. [Discussed at the 10:38 mark]

You can view the entire conversation in the following video:

Related:

tags: , , ,