O'Reilly logo

Testing in Scala by Daniel Hinojosa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required