
625
When to Use It
We can use a Testcase Class per Feature when we have a signifi cant number of Test
Methods and we want to make the specifi cation of each feature of the SUT more
obvious. Unfortunately, Testcase Class per Feature does not make each individual
Test Method any simpler or easier to understand; only Testcase Class per Fixture
(page 631) helps on that front. Likewise, it doesn’t make much sense to use
Testcase Class per Feature when each feature of the SUT requires only one or two
tests; in that case, we can stick with a single Testcase Class per Class (page 617).
Note that having a large number of features on a class is a “smell” indicating
the possibility that the class might have too many responsibilities. We typically
use Testcase Class per Feature when we are writing customer tests for methods
on a service Facade [GOF].
Variation: Testcase Class per Method
When a class has methods that take a lot of different parameters, we may have
many tests for the one method. We can group all of these Test Methods onto a
single Testcase Class per Method and put the rest of the Test Methods onto one
or more other Testcase Classes.
Variation: Testcase Class per Feature
Although a “feature” of a class is typically a single operation or function, it may
also be a set of related methods that operate on the same instance variable of
the object. For example, the set and get methods of a Java Bean would be con-
sidered a single (and trivial) “feature” of the class that contains those methods.
Similarly, a Data Access Object [CJ2EEP] would provide methods to both read
and write objects. It is diffi cult to test these methods in isolation, so we can treat
the reading and writing of one kind of object as a feature.
Variation: Testcase Class per User Story
If we are doing highly incremental development (such as we might do with eXtreme
Programming), it can be useful to put the new Test Methods for each story into a
different Testcase Class. This practice prevents commit-related confl icts when dif-
ferent people are working on different stories that affect the same SUT class. The
Testcase Class per User Story pattern may or may not end up being the same as
Testcase Class per Feature or Testcase Class per Method, depending on how we
partition our user stories.
Testcase Class per Feature
Testcase
Class per
Feature