
the SUT, and yet others to verify the post-test state of the SUT. Clearly identifying
the four phases makes the intent of the test much easier to see.
The fi xture setup phase of the test establishes the SUT’s state prior to the test,
which is an important input to the test. The exercise SUT phase is where we actu-
ally run the software we are testing. When reading the test, we need to see which
software is being run. The result verifi cation phase of the test is where we specify
the expected outcome. The fi nal phase, fi xture teardown, is all about housekeeping.
We wouldn’t want to obscure the important test logic with it because it is com-
pletely irrelevant from the perspective of Tests as Documentation (see page 23).
We should avoid the temptation to test as much functionality as pos-
sible in a single Test Method (page 348) because that can result in Obscure
Tests (page 186). In fact, it is preferable to have many small Single-Condition
Tests (see page 45). Using comments to mark the phases of a Four-Phase Test
is a good source of self-discipline, in that it makes it very obvious when our
tests are not Single-Condition Tests. It will be self-evident if we have multiple
exercise SUT phases separated by result verifi cation phases or if we have inter-
spersed fi xture setup and exercise SUT phases. Sure, the tests may work—but
they will provide less Defect Localization (see page 22) than if we have a bunch
of independent Single-Condition Tests.
Implementation Notes
We have several options for implementing the Four-Phase Test. In the simplest
case, each test is completely free-standing. All four phases of the test are con-
tained within the body of the Test Method. This structure implies we are using
In-line Setup (page 408) and either Garbage-Collected Teardown (page 500) or
In-line Teardown (page 509). It is the most appropriate choice when we are using
Testcase Class per Class (page 617) or Testcase Class per Feature (page 624) to
organize our Test Methods.
The other choice is to take advantage of the Test Automation Framework’s
(page 298) support for Implicit Setup (page 424) and Implicit Teardown (page 516).
We factor out the common fi xture setup and fi xture teardown logic into setUp and
tearDown methods on the Testcase Class (page 373). This leaves only the exercise SUT
and result verifi cation phases in the Test Method. This approach is an appropriate
choice when we are using Testcase Class per Fixture (page 631). We can also use
this approach to set up common parts of the fi xture when using Testcase Class per
Class (page 617) or Testcase Class per Feature or to tear down the fi xture when
using Automated Teardown (page 503).
Four-Phase
Test
Four-Phase Test
359