ScalaTest User Guide
Selecting testing styles
Defining base classes
Writing your first test
Tagging your tests
Running your tests
Testing with mock objects
Philosophy and design
Migrating to 2.0
Tests as specifications
In the behavior-driven development (BDD) style of testing, test suites act as executable specifications—specifications of system behavior that can be executed to verify that behavior. ScalaTest provides rich support for this style of testing.
In BDD, test names are sentences that specify a bit of desired behavior that the body of the test will ensure is working. This keeps tests focused
on just one thing, making it easier to figure out what behavior has been broken when a test fails. To keep the focus on specifying behavior, the word
“test” disappears from the source code and is replaced with words that make the code feel more like a specification. For example, in ScalaTest's
import org.scalatest.FunSpec import scala.collection.mutable.Stack
In addition to source code that reads more like a specification, the BDD approach also
encourages that test run reports also appear more like specifications. In addition to simply
reporting results, specification-style reports can facilitate communication about the system being tested
among stakeholders such as developers, managers, and customers.
For example, were you to run
scala> (new StackSpec).execute()
You would see ScalaTest's standard-out reporter display the results as an easy to read as an informal specification of the subject being tested:
A Stack - should pop values in last-in-first-out order - should throw NoSuchElementException if an empty stack is popped
ScalaTest also gives you ways to specify with greater granularity than just the test names. For example,
mixing in trait
import org.scalatest.FunSpec import org.scalatest.GivenWhenThen import scala.collection.mutable.Stack
Were you to run this
A Stack - should pop values in last-in-first-out-order + Given a non-empty stack + When pop is invoked on the stack + Then the most recently pushed element should be returned + And the stack should have one less item than before - should throw NoSuchElementException if an empty stack is popped + Given an empty stack + When pop is invoked on the stack + Then NoSuchElementException should be thrown + And the stack should still be empty
ScalaTest provides many traits that facilitate BDD style: Compared to
Next, learn about property-based testing.
ScalaTest is brought to you by Bill Venners, with
contributions from several other folks. It is sponsored by
ScalaTest is free, open-source software released under the Apache 2.0 license.
Copyright © 2009-2013 Artima, Inc. All Rights Reserved.