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:

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:

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: