
155
many test methods as we have paths through the code—but how else can we
expect to achieve full code coverage? What makes this pattern manageable is
that we Isolate the SUT (see page 43) when we write unit tests for each class so
we only have to focus on paths through a single object. Also, because each test
should verify only a single path through the code, each test method should con-
sist of strictly sequential statements that describe what should happen on that
one path.
1
Another reason we Verify One Condition per Test (see page 45) is to
Minimize Test Overlap (see page 44) so that we have fewer tests to modify if we
later modify the behavior of the SUT.
Brian Marrick has developed an interesting compromise that I call “While
We’re at It,”
2
which leverages the test fi xture we already have set up to run
some additional checks and assertions. Marrick clearly marks these elements
with comments to indicate that if changes to the SUT obsolete that part of the
test, they can be easily deleted. This strategy minimizes the effort needed to
maintain the extra test code.
Test Methods and Testcase Classes
A Test Method needs to live on a Testcase Class. Should we put all our Test
Methods onto a single Testcase Class for the application? Or should we create a
Testcase Class for each Test Method? Of course, the right answer lies somewhere
between these two extremes, and it will change over the life of our project.
Testcase Class per Class
When we write our fi rst few Test Methods, we can put them all onto a single
Testcase Class. As the number of Test Methods increases, we will likely want to
split the Testcase Class so that one Testcase Class per Class (page 617) is tested,
which reduces the number of Test Methods per class (Figure 12.2). As those
Testcase Classes get too big, we usually split the classes further. In that case, we
need to decide which Test Methods to include in each Testcase Class.
1
A Test Method that contains Conditional Test Logic (page 200) is a sign of a test trying
to accommodate different circumstances because it does not have control of all indirect
inputs of the SUT or because it is trying to verify complex expected states on an in-line
basis within the Test Method.
2
He calls it “Just for Laughs” but I don’t fi nd that name very intent-revealing.
Test Methods and Testcase Classes