ScalaTest User Guide

Getting started

Selecting testing styles

Defining base classes

Writing your first test

Using assertions

Tagging your tests

Running your tests

Sharing fixtures

Sharing tests

Using matchers

Testing with mock objects

Property-based testing

Asynchronous testing

Using Scala-js

Using Inside

Using OptionValues

Using EitherValues

Using PartialFunctionValues

Using PrivateMethodTester

Using WrapWith

Philosophy and design

Migrating to 3.0

Writing your first test

1. In ScalaTest, you define tests inside classes that extend a style class such as AnyFlatSpec (though in practice you'd usually directly extend a base class defined for your project, which extends a ScalaTest style class):

import org.scalatest.flatspec.AnyFlatSpec

class FirstSpec extends AnyFlatSpec { // tests go here... }

2. Each test in a AnyFlatSpec is composed of a sentence that specifies a bit of required behavior and a block of code that tests it. The sentence needs a subject, such as "A Stack"; a verb, either should, must, or can; and the rest of the sentence. Here's an example:

"A Stack" should "pop values in last-in-first-out order"

If you have multiple tests about the same subject, you can use it to refer to the previous subject:

it should "throw NoSuchElementException if an empty stack is popped"

After the sentence you put the word in followed by the body of the test in curly braces. Here's a complete example:

import collection.mutable.Stack
import org.scalatest.flatspec.AnyFlatSpec

class StackSpec extends AnyFlatSpec {
"A Stack" should "pop values in last-in-first-out order" in { val stack = new Stack[Int] stack.push(1) stack.push(2) assert(stack.pop() === 2) assert(stack.pop() === 1) }
it should "throw NoSuchElementException if an empty stack is popped" in { val emptyStack = new Stack[String] assertThrows[NoSuchElementException] { emptyStack.pop() } } }

3. Place this in a file called StackSpec.scala and compile it using this Jar file:

$ scalac -cp scalatest-app_3-3.2.18.jar StackSpec.scala

r. To run it, you will need one more artifact, the Jar file for Scala's XML module. Once you've downloaded that Jar file, you can run StackSpec using ScalaTest's Runner like this:

$ CLASSPATH=scalatest-app_3-3.2.18.jar:scala-xml_2.13-2.1.0.jar

$ scala -cp $CLASSPATH org.scalatest.run StackSpec
Run starting. Expected test count is: 2
StackSpec:
A Stack 
- should pop values in last-in-first-out order
- should throw NoSuchElementException if an empty stack is popped
Run completed in 96 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 2, failed 0, ignored 0, pending 0
All tests passed.

5. Or you can run it from the Scala interpreter using the ScalaTest shell:

$ scala -cp .:scalatest-app_3-3.2.18.jar:scala-xml_2.13-2.1.0.jar

scala> import org.scalatest._
import org.scalatest._

scala> run(new StackSpec)
StackSpec:
A Stack 
- should pop values in last-in-first-out order
- should throw NoSuchElementException if an empty stack is popped

Next, learn about using assertions.

ScalaTest is brought to you by Bill Venners and Artima.
ScalaTest is free, open-source software released under the Apache 2.0 license.

If your company loves ScalaTest, please consider sponsoring the project.

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

artima