
63
hash tables instead of tables. Each test may require many, many database opera-
tions to set up and tear down the fi xture required by a single query in the SUT.
There is a lot to be said for minimizing the size and complexity of the test
fi xture. A Minimal Fixture (see Minimal Fixture) is much easier to understand
and helps highlight the cause–effect relationship between the fi xture and the ex-
pected outcome. In this regard, it is a major enabler of Tests as Documentation
(page 23). In some cases, we can make the test fi xture much smaller by using
Entity Chain Snipping (see Test Stub on page 529) to eliminate the need to in-
stantiate those objects on which our test depends only indirectly. This tactic will
certainly speed up the instantiation of our test fi xture.
Shared Fixture Strategies
Sometimes we cannot—or choose not to—use a Fresh Fixture strategy. In these
cases, we can use a Shared Fixture. In this approach, many tests reuse the same
instance of a test fi xture.
The major advantage of Shared Fixtures is that we save a lot of execution
time in setting up and tearing down the fi xture. The main disadvantage is con-
veyed by one of its aliases, Stale Fixture, and by the test smell that describes its
most common side effects, Interacting Tests (see Erratic Test). Although Shared
Fixtures do have other benefi ts, most can be realized by applying other patterns
to Fresh Fixtures; Standard Fixture (page 305) avoids the fi xture design and
coding effort for every test without actually sharing the fi xture.
Now, if Shared Fixtures are so bad, why even discuss them? Because every-
one seems to go down this road at least once in his or her career—so we might
as well share the best available information about them should you venture
down that path. Mind you, this discussion isn’t meant to encourage anyone to
go down this path unnecessarily because it is paved with broken glass, infested
with poisonous snakes, and . . . well, you get my drift.
Given that we have decided to use a Shared Fixture (we did investigate every
possible alternative, didn’t we?), what are our options? We can make the fol-
lowing adjustments (Figure 6.5):
• How far and wide we share a fi xture (e.g., a Testcase Class, all tests in
a test suite, all test run by a particular user)
• How often we recreate the fi xture
Which Test Fixture Strategy Do We Use?