
724
Chapter 27  Value Patterns
in Nondeterministic Tests (see Erratic Test); if we encounter nondeterminism 
(sometimes the test passes and then fails during the very next run), we must 
check the SUT code to see whether differences in value could be the root cause. 
In general, we shouldn’t use a Generated Value unless the value must be 
unique because of the nondeterminism such a value may introduce. The obvi-
ous alternative is to use a Literal Value. A less obvious alternative is to use a 
Derived Value (page 718), especially when we must determine the expected 
results of a test. 
Implementation Notes 
We can generate values in a number of ways. The appropriateness of each tech-
nique depends on the circumstance. 
Variation: Distinct Generated Value 
When we need to ensure that each test or object uses a different value, we can take 
advantage of Distinct Generated Values. In such a case, we can create a set of util-
ity functions that will return unique values of various types (e.g., integers, strings, 
fl oating-point numbers). The various getUnique methods can all be built upon an 
integer sequence number generator. For numbers that must be unique within the 
scope of a shared database, we can use database sequences or a sequence table. 
For numbers that must be unique within the scope of a particular test run, we can 
use an in-memory sequence number generator (e.g., use a Java static variable that 
is incremented before usage). In-memory sequence numbers that start from the 
number 1 each time a test suite is run offer a useful quality: The values generated 
in each test are the same for each run and can simplify debugging. 
Variation: Random Generated Value 
One way to obtain good test coverage without spending a lot of time analyzing 
the behavior and generating test conditions is to use different values each time 
we run the tests. Using a Random Generated Value is one way to accomplish 
this goal. While use of such values may seem like a good idea, it makes the tests 
nondeterministic (Nondeterministic Tests) and can make debugging failed tests 
very diffi cult. Ideally, when a test fails, we want to be able to repeat that test 
failure on demand. To do so, we can log the Random Generated Value as the test 
is run and show it as part of the test failure. We then need to fi nd a way to force 
the test to use that value again while we are troubleshooting the failed test. In 
most cases, the effort required outweighs the potential benefi t. Of course, when 
we need this technique, we really need it. 
Generated
Value