Sharing tests
Sometimes you may want to run the same test code on different fixture objects. In other words, you may want to write tests that are "shared"
by different fixture objects. To accomplish this in a AnyFlatSpec
, you first place shared tests in behavior functions.
These behavior functions will be invoked during the construction phase of any AnyFlatSpec
that uses them, so that the tests they
contain will be registered as tests in that AnyFlatSpec
. For example, given this stack class:
import scala.collection.mutable.ListBuffer
class Stack[T] {
val MAX = 10
private val buf = new ListBuffer[T]
def push(o: T) {
if (!full)
buf.prepend(o)
else
throw new IllegalStateException("can't push onto a full stack")
}
def pop(): T = {
if (!empty)
buf.remove(0)
else
throw new IllegalStateException("can't pop an empty stack")
}
def peek: T = {
if (!empty)
buf(0)
else
throw new IllegalStateException("can't pop an empty stack")
}
def full: Boolean = buf.size == MAX
def empty: Boolean = buf.size == 0
def size = buf.size
override def toString = buf.mkString("Stack(", ", ", ")")
}
You may want to test the Stack
class in different states: empty, full, with one item, with one item less than capacity,
etc. You may find you have several tests that make sense any time the stack is non-empty. Thus you'd ideally want to run
those same tests for three stack fixture objects: a full stack, a stack with a one item, and a stack with one item less than
capacity. With shared tests, you can factor these tests out into a behavior function, into which you pass the
stack fixture to use when running the tests. So in your AnyFlatSpec
for stack, you'd invoke the
behavior function three times, passing in each of the three stack fixtures so that the shared tests are run for all three fixtures. You
can define a behavior function that encapsulates these shared tests inside the AnyFlatSpec
that uses them. If they are shared
between different AnyFlatSpec
s, however, you could also define them in a separate trait that is mixed into each AnyFlatSpec
that uses them.
For example, here the nonEmptyStack
behavior function (in this case, a behavior method) is
defined in a trait along with another method containing shared tests for non-full stacks:
trait StackBehaviors { this: AnyFlatSpec =>
def nonEmptyStack(newStack: => Stack[Int], lastItemAdded: Int) {
it should "be non-empty" in {
assert(!newStack.empty)
}
it should "return the top item on peek" in {
assert(newStack.peek === lastItemAdded)
}
it should "not remove the top item on peek" in {
val stack = newStack
val size = stack.size
assert(stack.peek === lastItemAdded)
assert(stack.size === size)
}
it should "remove the top item on pop" in {
val stack = newStack
val size = stack.size
assert(stack.pop === lastItemAdded)
assert(stack.size === size - 1)
}
}
def nonFullStack(newStack: => Stack[Int]) {
it should "not be full" in {
assert(!newStack.full)
}
it should "add to the top on push" in {
val stack = newStack
val size = stack.size
stack.push(7)
assert(stack.size === size + 1)
assert(stack.peek === 7)
}
}
}
Given these behavior functions, you could invoke them directly, but AnyFlatSpec
offers a DSL for the purpose,
which looks like this:
it should behave like nonEmptyStack(stackWithOneItem, lastValuePushed)
it should behave like nonFullStack(stackWithOneItem)
If you prefer to use an imperative style to change fixtures, for example by mixing in BeforeAndAfterEach
and
reassigning a stack
var
in beforeEach
, you could write your behavior functions
in the context of that var
, which means you wouldn't need to pass in the stack fixture because it would be
in scope already inside the behavior function. In that case, your code would look like this:
it should behave like nonEmptyStack
it should behave like nonFullStack
The recommended style, however, is the functional, pass-all-the-needed-values-in style. Here's an example:
class SharedTestExampleSpec extends AnyFlatSpec with StackBehaviors {
def emptyStack = new Stack[Int]
def fullStack = {
val stack = new Stack[Int]
for (i <- 0 until stack.MAX)
stack.push(i)
stack
}
def stackWithOneItem = {
val stack = new Stack[Int]
stack.push(9)
stack
}
def stackWithOneItemLessThanCapacity = {
val stack = new Stack[Int]
for (i <- 1 to 9)
stack.push(i)
stack
}
val lastValuePushed = 9
"A Stack (when empty)" should "be empty" in {
assert(emptyStack.empty)
}
it should "complain on peek" in {
intercept[IllegalStateException] {
emptyStack.peek
}
}
it should "complain on pop" in {
intercept[IllegalStateException] {
emptyStack.pop
}
}
"A Stack (with one item)" should behave like nonEmptyStack(stackWithOneItem, lastValuePushed)
it should behave like nonFullStack(stackWithOneItem)
"A Stack (with one item less than capacity)" should
behave like nonEmptyStack(stackWithOneItemLessThanCapacity, lastValuePushed)
it should behave like nonFullStack(stackWithOneItemLessThanCapacity)
"A Stack (full)" should "be full" in {
assert(fullStack.full)
}
it should behave like nonEmptyStack(fullStack, lastValuePushed)
it should "complain on a push" in {
intercept[IllegalStateException] {
fullStack.push(10)
}
}
}
If you load these classes into the Scala interpreter (with scalatest's JAR file on the class path), and execute it,
you'll see:
scala> (new SharedTestExampleSpec).execute()
A Stack (when empty)
- should be empty
- should complain on peek
- should complain on pop
A Stack (with one item)
- should be non-empty
- should return the top item on peek
- should not remove the top item on peek
- should remove the top item on pop
- should not be full
- should add to the top on push
A Stack (with one item less than capacity)
- should be non-empty
- should return the top item on peek
- should not remove the top item on peek
- should remove the top item on pop
- should not be full
- should add to the top on push
A Stack (full)
- should be full
- should be non-empty
- should return the top item on peek
- should not remove the top item on peek
- should remove the top item on pop
- should complain on a push
One thing to keep in mind when using shared tests is that in ScalaTest, each test in a suite must have a unique name.
If you register the same tests repeatedly in the same suite, one problem you may encounter is an exception at runtime
complaining that multiple tests are being registered with the same test name. A good way to solve this problem in a AnyFlatSpec
is to make sure
each invocation of a behavior function is in the context of a different subject,
which will prepend a string to each test name.
For example, the following code in a AnyFlatSpec
would register a test with the name "A Stack (when empty) should be empty"
:
behavior of "A Stack (when empty)"
it should "be empty" in {
assert(emptyStack.empty)
}
Or, using the shorthand notation:
"A Stack (when empty)" should "be empty" in {
assert(emptyStack.empty)
}
If the "should be empty"
test was factored out into a behavior function, it could be called repeatedly so long
as each invocation of the behavior function is in the context of a different subject.
Shared tests in other style traits
Shared tests are supported in all style traits in which tests are represented as functions, because they require
registering the same test function multiple times, each time parameterized with different fixture objects. In trait Suite
tests
are methods, thus shared tests aren't supported. The Scaladoc of each style trait that supports them includes an example of shared tests.
A related technique is property-based testing. Whereas in shared tests you evaluate the
same test function on different data, in property-based testing you evaluate the same property function on different data.
Next, learn about using matchers.