
the interpreter retrieves the test data from the fi le and sets up the test fi xture us-
ing the data from the fi le. Second, it exercises the SUT with whatever arguments
the fi le specifi es. Third, it compares the actual results produced by the SUT (e.g.,
returned values, post-test state) with the expected results from the fi le. If the
results don’t match, it marks the test as failed; if the SUT throws an exception,
it catches the exception and marks the test accordingly and continues. Fourth,
the interpreter does any fi xture teardown that is necessary and then moves on to
the next test in the fi le.
A test that might otherwise require a series of complex steps can be reduced
to a single line of data in the Data-Driven Test fi le. Fit is a popular example of
a framework for writing Data-Driven Tests.
When to Use It
A Data-Driven Test is an alternative strategy to a Recorded Test (page 278) and
a Scripted Test (page 285). It can also be used as part of a Scripted Test strategy,
however, and Recorded Tests are, in fact, Data-Driven Tests when they are played
back. A Data-Driven Test is an ideal strategy for getting business people involved
in writing automated tests. By keeping the format of the data fi le simple, we make
it possible for the business person to populate the fi le with data and execute the
tests without having to ask a technical person to write test code for each test.
We can consider using a Data-Driven Test as part of a Scripted Test strategy
whenever we have a lot of different data values with which we wish to exercise
the SUT where the same sequence of steps must be executed for each data value.
Usually, we discover this similarity over time and refactor fi rst to a Parameterized
Test (page 607) and then to a Data-Driven Test. We may also want to arrange a
standard set of steps in different sequences with different data values much like
in an Incremental Tabular Test (see Parameterized Test). This approach gives us
the best coverage with the least amount of test code to maintain and makes it
very easy to add more tests as they are needed.
Another consideration when deciding whether to use Data-Driven Tests is
whether the behavior we are testing is hard-coded or driven by confi guration
data. If we automate tests for data-driven behavior using Scripted Tests, we must
update the test programs whenever the confi guration data changes. This behavior
is just plain unnatural because it implies that we must commit changes to our
source code repository [SCM] whenever we change the data in our confi guration
database.
4
By making the tests data-driven, changes to the confi guration data or
4
Of course, we should be managing our test data in a version-controlled Repository,
too—but that topic could fi ll another book; see [RDb] for details.
Data-Driven Test
Data-Driven
Test
289