Data Driven Testing
Data Driven Testing
Data-driven testing is testing where data contained in an input test data file control the flow and actions
performed by the automated test script.
[Just Enough Software Test Automation, page 106]
Introduction
This article demonstrates how the open source Wiki FitNesse can be used to implement data-
driven testing, making tests at once more efficient and easier to maintain. The examples are in
Java, but FitNesse supports other languages.
I assume that you have FitNesse up and running (very easy).
An Example
This example will demonstrate how TestFirst can improve quality and how easy data-driven
testing with FitNesse can make this. I will start with the traditional approach of presenting the
specification, the code and then the (data-driven) tests. These tests will show several problems
and I will then restart using a TestFirst approach.
TestLast Approach
We are working on a banking application and one module is to calculate compound interest.
The specification states:
package testbuch;
import junit.framework.TestCase;
TestFirst Approach
To understand whether the code above does the right thing, we would need to know how it is
used. It does return doubles for once. Therefore:
System.out.println(CompoundInterestCalculator.
calculateCompoundInterest(100, 0.1, 1));
110.00000000000001
This might be okay if it is used only for further calculations, but is definitely not what we
want to show on a GUI. And what about entering negative numbers? We could use them to
calculate backwards (how much would I need to achieve some capital), but most likely
negative values are to be rejected. Therefore we need to clarify our requirements. And we
need more tests. In this case it is very easy to calculate new test data but in real live test data
is usually hard to get. And even now we see how the test data is included in our test code.
I once sad three days with a requirements engineering consultant over our requirements and
learned how hard it can be to express oneself clearly and without double meanings. Being
lazy, we take the easy way out and only define some examples. We do this in Fitnesse, of
course.
package testbuch;
import fit.ColumnFixture;
Figure 0.2
And the best thing is that we can let the customer or the business analyst define these tests.
Now it becomes their responsibility to define exactly what the software is to do. No more “I
wanted something else” as they have to define their wishes with mathematical precision now!
Conclusion
This article demonstrated how to use a data-driven test to improve Java code by providing test
cases in a table. These tests define the requirement much more precise than words can do. The
example used in this article refactored a “normal” JUnit test to a FitNesse driven test, showing
how much more easy testing can get.