
442
say, “method on the fi rst/fi nal Testcase Object” but that isn’t true: NUnit, unlike
other members of the xUnit family, creates only a single Testcase Object. See the
sidebar “There’s Always an Exception” on page 384 for details.)
When to Use It
We can use Suite Fixture Setup when we have a test fi xture we wish to share
between all Test Methods of a single Testcase Class and our variant of xUnit sup-
ports this feature. This pattern is particularly useful if we need to tear down the
fi xture after the last test is run. At the time of writing this book, only VbUnit,
NUnit, and JUnit 4.0 supported Suite Fixture Setup “out of the box.” Nevertheless,
it is not diffi cult to add this capability in most variants of xUnit.
If we need to share the fi xture more widely, we must use either a Prebuilt
Fixture (page 429), a Setup Decorator (page 447), or Lazy Setup (page 435).
If we don’t want to share the actual instance of the fi xture but we do want to
share the code to set up the fi xture, we can use Implicit Setup (page 424) or
Delegated Setup (page 411).
The main reason for using a Shared Fixture, and hence Suite Fixture Setup,
is to overcome the problem of Slow Tests (page 253) caused by too many test
fi xture objects being created each time every test is run. Of course, a Shared
Fixture can lead to Interacting Tests (see Erratic Test on page 228) or even
a Test Run War (see Erratic Test); the sidebar “Faster Tests Without Shared
Fixtures” (page 319) describes other ways to solve this problem.
Implementation Notes
For Suite Fixture Setup to work properly, we must ensure that the fi xture is
remembered between calls to the Test Methods. This criterion implies we need to
use a class variable, Registry [PEAA], or Singleton [GOF] to hold the references
to the fi xture (except in NUnit; see the sidebar “There’s Always an Exception”
on page 384). The exact implementation varies from one member of the xUnit
family to the next. Here are a few highlights:
• In VbUnit, we implement the interface IFixtureFrame in the Testcase Class,
thereby causing the Test Automation Framework (1) to call the IFixture
Frame_Create
method before the fi rst Test Method is called and (2) to call
the IFixtureFrame_Destroy method after the last Test Method is called.
• In NUnit, the attributes [TestFixtureSetUp] and [TestFixtureTearDown] are
used inside a test fi xture to designate the methods to be called (1) once
Suite
Fixture
Setup
Chapter 20 Fixture Setup Patterns