There are multiple libraries and testing methodologies for Scala, but in this tutorial, we’ll demonstrate one popular option from the ScalaTest framework called AnyFunSuite. We assume you know how to create a Scala project with sbt.
Setup
- On the command line, create a new directory somewhere.
cdinto the directory and runsbt new scala/scalatest-example.g8- Name the project
ScalaTestTutorial. - The project comes with ScalaTest as a dependency in the
build.sbtfile. cdinto the directory and runsbt test. This will run the test suiteCubeCalculatorTestwith a single test calledCubeCalculator.cube.
sbt test
[info] Loading global plugins from /Users/username/.sbt/0.13/plugins
[info] Loading project definition from /Users/username/workspace/sandbox/my-something-project/project
[info] Set current project to scalatest-example (in build file:/Users/username/workspace/sandbox/my-something-project/)
[info] CubeCalculatorTest:
[info] - CubeCalculator.cube
[info] Run completed in 267 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed Feb 2, 2017 7:37:31 PM
Understanding tests
- Open up two files in a text editor:
src/main/scala/CubeCalculator.scalasrc/test/scala/CubeCalculatorTest.scala
- In the file
CubeCalculator.scala, you’ll see how we define the functioncube. - In the file
CubeCalculatorTest.scala, you’ll see that we have a class named after the object we’re testing.
import org.scalatest.funsuite.AnyFunSuite
class CubeCalculatorTest extends AnyFunSuite {
test("CubeCalculator.cube") {
assert(CubeCalculator.cube(3) === 27)
}
}
Let’s go over this line by line.
class CubeCalculatorTestmeans we are testing the objectCubeCalculatorextends AnyFunSuitelets us use functionality of ScalaTest’s AnyFunSuite class such as thetestfunctiontestis function that comes from AnyFunSuite that collects results from assertions within the function body."CubeCalculator.cube"is a name for the test. You can call it anything but one convention is “ClassName.methodName”.asserttakes a boolean condition and determines whether the test passes or fails.CubeCalculator.cube(3) === 27checks whether the output of thecubefunction is indeed 27. The===is part of ScalaTest and provides clean error messages.
Adding another test case
-
Add another test block with its own
assertstatement that checks for the cube of0.import org.scalatest.funsuite.AnyFunSuite class CubeCalculatorTest extends AnyFunSuite { test("CubeCalculator.cube 3 should be 27") { assert(CubeCalculator.cube(3) === 27) } test("CubeCalculator.cube 0 should be 0") { assert(CubeCalculator.cube(0) === 0) } } -
Execute
sbt testagain to see the results.sbt test [info] Loading project definition from C:\projects\scalaPlayground\scalatestpractice\project [info] Loading settings for project root from build.sbt ... [info] Set current project to scalatest-example (in build file:/C:/projects/scalaPlayground/scalatestpractice/) [info] Compiling 1 Scala source to C:\projects\scalaPlayground\scalatestpractice\target\scala-2.13\test-classes ... [info] CubeCalculatorTest: [info] - CubeCalculator.cube 3 should be 27 [info] - CubeCalculator.cube 0 should be 0 [info] Run completed in 257 milliseconds. [info] Total number of tests run: 2 [info] Suites: completed 1, aborted 0 [info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed. [success] Total time: 3 s, completed Dec 4, 2019 10:34:04 PM
Conclusion
You’ve seen one way to test your Scala code. You can learn more about ScalaTest’s FunSuite on the official website. You can also check out other testing frameworks such as ScalaCheck and Specs2.