Wednesday, April 16, 2008

Iterating through arrays in TDD

One of the patterns that I frequently follow in Test Driven Development (TDD) that I haven't seen mentioned elsewhere (yet) and that I find extremely effective is to create arrays of potential inputs for parameters to functions that I'm testing and then iterate through those arrays calling the same function and testing the results.

For example, if I had a function ConcatString(string one, string two) I could test it as such:

            string[] one = { "black", "blue", "orange" };
            string[] two = { "yellow", "white", "brown", "cotton" };
            for (int i = 0; i < one.Length; i++)
                for (int j = 0; j < two.Length; j++)
                    string actual = ConcatString(one[i], two[j]);
                    string expected = one[i] + two[j];
                    Assert.AreEqual(expected, actual);

No comments:

Post a Comment