
105
If we are happy with the idea of creating the test fi xture the fi rst time any test
needs it, we can use Lazy Setup (page 435) in the setUp method of the corre-
sponding Testcase Class to create it as part of running the fi rst test. Subsequent
tests will then see that the fi xture already exists and reuse it. Because there is no
obvious signal that the last test in a test suite (or Suite of Suites; see Test Suite
Object on page 387) has been run, we won’t know when to tear down the fi x-
ture after each test run. This can lead to Unrepeatable Tests because the fi xture
may survive across test runs (depending on how the various tests access it).
If we need to share the fi xture more broadly, we could include a Fixture Set-
up Testcase at the beginning of the test suite. This is a special case of Chained
Tests and suffers from the same problem as Lazy Setup—specifi cally, we don’t
know when it is time to tear down the fi xture. It also depends on the ordering
of tests within a suite, so it works best with Test Enumeration (page 399).
If we need to be able to tear down the test fi xture after running a test suite,
we must use a fi xture management mechanism that tells us when the last test
has been run. Several members of the xUnit family support the concept of a
setUp method that runs just once for the test suite created from a single Testcase
Class. This Suite Fixture Setup (page 441) method has a corresponding tearDown
method that is called when the last Test Method has fi nished running.
5
We can
then guarantee that a new fi xture is built for each test run. The fi xture is not left
over to cause problems with subsequent test runs, which prevents Unrepeatable
Tests; it does not prevent Interacting Tests within the test run, however. This
capability could be added as an extension to any member of the xUnit family.
When it isn’t supported or when we need to share the fi xture beyond a single
Testcase Class, we can resort to using a Setup Decorator (page 447) to bracket
the running of a test suite with the execution of the fi xture setUp and tearDown
logic. The biggest drawback of Setup Decorator is that tests that depend on the
decorator cannot be run by themselves; they are Lonely Tests.
The fi nal option is to build the fi xture well before the tests are run—that is,
to employ a Prebuilt Fixture (page 429). This approach offers the most options
regarding how the test fi xture is actually constructed because the fi xture setup
need not be executable from within xUnit. For example, it could be set up manu-
ally, by using database scripts, by copying a “golden” database, or by running
a data generation program. The major disadvantage with a Prebuilt Fixture
is that if any tests are Unrepeatable Tests, we will need to perform a Manual
Intervention (page 250) before each test run. As a result, a Prebuilt Fixture is of-
ten used in combination with a Fresh Fixture to construct an Immutable Shared
Fixture (see Shared Fixture).
5
Think of it as a built-in decorator for a single Testcase Class.
Managing Shared Fixtures