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 FlatSpec, you first place shared tests in behavior functions.
These behavior functions will be invoked during the construction phase of any FlatSpec that uses them, so that the tests they
contain will be registered as tests in that FlatSpec. For example, given this stack class:

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 FlatSpec
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 FlatSpec that uses them. If they are shared
between different FlatSpecs, however, you could also define them in a separate trait that is mixed into each FlatSpec
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:

Given these behavior functions, you could invoke them directly, but FlatSpec 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 stackvar 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 // assuming lastValuePushed is also in scope inside nonEmptyStack
it should behave like nonFullStack

The recommended style, however, is the functional, pass-all-the-needed-values-in style. Here's an example:

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 FlatSpec 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 FlatSpec would register a test with the name "A Stack (when empty) should be 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.