
456
test reader—a classic case of a Mystery Guest (see Obscure Test on page 186).
This problem can be at least partially mitigated through the use of appropri-
ately named Finder Methods (see Test Utility Method) to access the objects in
the Shared Fixture. It is less of an issue if all the Test Methods are on the same
Testcase Class (page 373) and are listed in the same order as they are executed.
Variation: Fixture Setup Testcase
If we need to set up a Shared Fixture and we cannot use any of the other tech-
niques to set it up [e.g., Lazy Setup (page 435), Suite Fixture Setup (page 441),
or Setup Decorator (page 447)], we can arrange to have a Fixture Setup Testcase
run as the fi rst test in the test suite. This is simple to do if we are using Test Enu-
meration (page 399); we just include the appropriate addTest method call in our
Test Suite Factory (see Test Enumeration). This variation is a degenerate form
of the Chained Tests pattern in that we are chaining a test suite behind a single
Fixture Setup Testcase.
Implementation Notes
There are two key challenges in implementing Chained Tests:
• Getting tests in the test suite to run in the desired order
• Accessing the fi xture leftover by the previous test(s)
While a few members of the xUnit family provide an explicit mechanism for
defi ning the order of tests, most members make no such guarantees about this
order. We can probably fi gure out what order the xUnit member uses by per-
forming a few experiments. Most commonly, we will discover that it is either the
order in which the Test Methods appear in the fi le or alphabetical order by Test
Method name (in which case, the easiest solution is to include a test sequence
number in the test name). In the worst-case scenario, we could always revert
to Test Method Enumeration (see Test Enumeration) to ensure that Testcase
Objects are added to the test suite in the correct order.
To refer to the objects created by the previous tests, we need to use one of the
fi xture object access patterns. If the preceding tests are Test Methods on the same
Testcase Class, it is suffi cient for each test to store any object references that subse-
quent tests will use to access the fi xture in a fi xture holding class variable. (Fixture
holding instance variables typically won’t work here because each test runs on a
separate Testcase Object and, therefore, the tests don’t share instance variables.
See the sidebar “There’s Always an Exception” on page 384 for a description of
when instance variations do not behave this way.)
Chained
Tests
Chapter 20 Fixture Setup Patterns