Getting started with FunSuite

By learning to use FunSuite, simple assertions, and the BeforeAndAfter trait, you can become productive in the TDD style of ScalaTest very quickly. You can then learn and use more of ScalaTest over time.

In a FunSuite, tests are function values. (The “Fun” in FunSuite stands for function.) You denote tests with test and provide the name of the test as a string enclosed in parentheses, followed by the code of the test in curly braces. Here's an example:

import org.scalatest.FunSuite
import scala.collection.mutable.Stack
 
class ExampleSuite extends FunSuite {
 
  test("pop is invoked on a non-empty stack") {
 
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    val oldSize = stack.size
    val result = stack.pop()
    assert(result === 2)
    assert(stack.size === oldSize - 1)
  }
 
  test("pop is invoked on an empty stack") {
 
    val emptyStack = new Stack[Int]
    intercept[NoSuchElementException] {
      emptyStack.pop()
    }
    assert(emptyStack.isEmpty)
  }
}

You can compile this FunSuite like this:

$ scalac -cp scalatest-2.2.0.jar ExampleSuite.scala

Here's how you run it:

$ scala -cp scalatest-2.2.0.jar org.scalatest.run ExampleSuite
Run starting. Expected test count is: 2
ExampleSuite:
- pop is invoked on a non-empty stack
- pop is invoked on an empty stack
Run completed in 73 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, ignored 0, pending 0
All tests passed.

Using assertions

To get started quickly with ScalaTest, learn to use assert with the === operator and intercept. Later if you prefer you can switch to ScalaTest's matchers.

ScalaTest lets you use Scala's assertion syntax, but defines a triple equals operator (===) to give you better error messages. The following code would give you an error indicating only that an assertion failed:

assert(1 == 2)

Using triple equals instead would give you the more informative error message, "1 did not equal 2":

assert(1 === 2)

To test whether a bit of code produces an expected exception, use intercept. Place the expected exception type in square brackets after intercept, and the bit of code in curly braces, like this:

val s = "hi"
intercept[IndexOutOfBoundsException] {
  s.charAt(-1)
}

If the bit of code between the curly braces throws the expected exception, intercept will return it. In the previous example that return value was ignored. If you want to inspect the thrown exception, you can do so like this:

val s = "hi"
val thrown = intercept[IndexOutOfBoundsException] {
  s.charAt(-1)
}
assert(thrown.getMessage === "String index out of range: -1")

If the bit of code between the curly braces throws the wrong exception, or does not throw any exception, you'll get a test failure describing the problem.

Factoring out duplicate code

If you want to factor out duplicate code from tests, mix in BeforeAndAfter, surround the code you want to run before each test by before { ... }, and after each test by after { ... }. Here's an example that just uses before:

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import scala.collection.mutable.Stack
 
class ExampleSuite extends FunSuite with BeforeAndAfter {
 
  var stack: Stack[Int] = _

  before {
    stack = new Stack[Int]
  }

  test("pop is invoked on a non-empty stack") {
 
    stack.push(1)
    stack.push(2)
    val oldSize = stack.size
    val result = stack.pop()
    assert(result === 2)
    assert(stack.size === oldSize - 1)
  }
 
  test("pop is invoked on an empty stack") {
 
    intercept[NoSuchElementException] {
      stack.pop()
    }
    assert(stack.isEmpty)
  }
}

You compile and run it the same way:

$ scalac -cp scalatest-2.2.0.jar ExampleSuite.scala
$ scala -cp scalatest-2.2.0.jar org.scalatest.run ExampleSuite
Run starting. Expected test count is: 2
ExampleSuite:
- pop is invoked on a non-empty stack
- pop is invoked on an empty stack
Run completed in 73 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, ignored 0, pending 0
All tests passed.

Armed with this knowledge, you can already write real TDD-style tests with ScalaTest. To go further, check out the user guide.

ScalaTest is brought to you by Bill Venners, with contributions from several other folks. It is sponsored by Artima, Inc.
ScalaTest is free, open-source software released under the Apache 2.0 license.

Copyright © 2009-2013 Artima, Inc. All Rights Reserved.

artima