
715
If the values in the expected outcome can be derived from the values in the
fi xture setup logic, we will be more likely to use the Tests as Documentation
(see page 23) if we use Derived Values (page 718). Conversely, if the values are
not important to the specifi cation of the logic being tested, we should consider
using Generated Values (page 723).
Implementation Notes
The most common way to use a Literal Value is with literal constants within
the code. When the same value needs to be used in several places in the test
(typically during fi xture setup and result verifi cation), this approach can
obscure the relationship between the test pre-conditions and post-conditions.
Introducing an evocatively named symbolic constant can make this relationship
much clearer. Likewise, if we cannot use a self-describing value, we can still make
the code easier to use by defi ning a suitably named symbolic constant and using
it wherever we would have used the Literal Value.
Variation: Symbolic Constant
When we need to use the same Literal Value in several places in a single Test
Method (page 348) or within several distinct tests, it is a good practice to use a
Symbolic Constant instead of a Literal Value. A Symbolic Constant is function-
ally equivalent to a Literal Value but reduces the likelihood of High Test Mainte-
nance Cost (page 265).
Variation: Self-Describing Value
When several attributes of an object need the same kind of value, using different
values provides advantages by helping us to prove that the SUT is working with
the correct attribute. When an attribute or argument is an unconstrained string,
it can be useful to choose a value that describes the role of the value in the test
(a Self-Describing Value). For example, using “Not an existing customer” for
the name of a customer might be more helpful to the reader than using “Joe
Blow,” especially when we are debugging or when the attributes are included in
the test failure output.
Example: Literal Value
Because Literal Value is usually the starting point when writing tests, I’ll dis-
pense with a motivating example and cut straight to the chase. Here’s an
example of the Literal Value pattern in action. Note the use of Literal Values
in both the fi xture setup logic and the assertion.
Literal Value
Literal Value