ScalaTest's Scala WordSpec Unit Testing Style

Published

scalatest

Prerequisites #

  • Scala, SBT, and Intellij IDE installed on laptop
  • Some understanding of Scala

Tutorial #

Before we begin with ScalaTest, let’s make sure we have a Scala project to work from. Assuming you have Scala and SBT installed, one of the easiest ways to scaffold a new Scala project is through Giter8. You can run the following sbt command:

C:\demos\scala>sbt new scala/hello-world.g8

Name your project at the prompt then open it in your IDE. Now, you can add the ScalaTest library dependency by adding the following line to your build.sbt file:

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test

ScalaTest is one of the most popular, complete and easy-to-use unit testing frameworks in the Scala ecosystem. WordSpec is one of several testing styles the ScalaTest library supports. WordSpec differentiates itself from the other testing styles as being very prescriptive in how text must be written, and so this testing style requires a higher degree of discipline.

Scala WordSpec facilitates a behavior-driven style of development. When writing tests with WordSpec, you write a one or more sentence specification for each bit of behavior you want to specify and test.

Subjects and Clauses #

The entity under test is called the subject. Identifying the subject in this style of testing is optional, but I highly recommend it. From the subject, you can create a when clause that can describe the subject in varying situations. For example, let us start with the following:

"A Set" when {} 

If you can’t think of what the subject may be, just put the name of the class under test until you can think of something better. Within this block, you can put one or more blocks describing the state or situation the subject may be in. For example:

"A Set" when {
"empty" should {}
}

Along with should, you can also use the verbs must or can and the test should function the same way. Now, you’re ready to write your first test case. This is done with what we’ll call an in clause. For example:

"A Set" when {
"empty" should {
"have size 0" in {
Set.empty.size should be (0)
}
}
}

Try It in Your IDE #

Notice, you don’t have to be an expert Scala developer to follow this. Also notice each of the clauses begin with a phrase of type String. It is a nice simple way to construct your clauses, but it is not the only way. The other options are outside of scope here.

The complete test class, I’ve uncreatively named ExampleSpec, would look something like this:

import org.scalatest.{Matchers, WordSpec}

class ExampleSpec extends WordSpec with Matchers {
"A Set" when {
"empty" should {
"have size 0" in {
Set.empty.size should be (0)
}
}
}
}

Feel free to add this test class to the Scala project you scaffolded above. You can create a test folder in your src directory and a scala folder within the test folder you created. Then, you can create this Scala class there like any other Scala class. You just have to right-click on the scala folder you created within the test folder and select New => Scala Class.

More Info on Github #

References #

  1. org.scalatest.WordSpec. https://www.scalatest.org/scaladoc/1.8/org/scalatest/WordSpec.html. Accessed 28 May 2021.
  2. org.scalatest.WordSpec. https://www.scalatest.org/at_a_glance/WordSpec. Accessed 31 May 2021.
  3. WordSpec - ScalaTest 3.0.0-M10 - org.scalatest.WordSpec. https://www.artima.com/docs-scalatest-3.0.0-M10/org/scalatest/WordSpec.html. Accessed 31 May 2021.
  4. Introduction to testing with ScalaTest. https://www.baeldung.com/scala/scalatest. Accessed 31 May 2021.
  5. Testing Scala in Intellij with ScalaTest. https://docs.scala-lang.org/getting-started/intellij-track/testing-scala-in-intellij-with-scalatest.html. Accessed May 2021.

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖!

For feedback, please ping me on Twitter.