Chapter 3. ScalaTest

ScalaTest is a popular testing framework created by programmer Bill Venners specifically for Scala. ScalaTest is an extensive behavior-driven development (BDD) suite with numerous built-in specs, but it also intergrates with some of the classic testing frameworks like JUnit and TestNG. ScalaTest also has two assertion dialects to choose from, depending how you want your test to read. ScalaTest’s learning curve is fast, and it runs automatically in the test plug-in installed with SBT.

ScalaTest offers several different flavors of tests. The most basic one is the FunSpec, which we used to add an Artist to an Album in our introduction. It contains a standard storyboard that describes the reason for the existence of the test using a describe clause and subsequent tests that fulfill that description. As we saw in our introductory chapter, AlbumTest.scala took the form:

package com.oreilly.testingscala

import org.scalatest.FunSpec
import org.scalatest.matchers.ShouldMatchers

class AlbumTest extends FunSpec with ShouldMatchers  {
   describe("An Album") {
       it ("can add a Artist object to the album") {
           val album = new Album("Thriller", 1981,
              new Artist("Michael", "Jackson"))
       album.artist.firstName should be ("Michael")
       }
   }
}

AlbumTest in this example extends FunSpec. This is a standard BDD specification. FunSpec is a trait, which means that is mixed into a class. This mixed-in trait provides us with a few methods to run our test: describe and it. describe is the subject of ...

Get Testing in Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.