Friday, 8 December 2006

Test Driven Development

My first blog ... time to dive in. Here goes.

Automated Unit testing has saved me many times over. For the time you spend, versus the time you save it is a solid investment. Whether you use nunit or Visual Studio Team Framework - it is up to you. But it is well worth the time up front. How many times do you think - oh the requirements won't change (or if they change it won't be that difficult to keep track of or fix my code). For example ... take the case when you are comparing lines upon lines of xml output. Or even worse, let's say you have to use fairly complex (1000+ characters) regular expressions to match multi-delimiter delimited data and use that to generate xml. A few lines of code ... and you save heaps of time ... I have written a very simple function (called XmlCompare) that uses XPathNodeIterator to perform the following Xml Walkthrough of Expected and Actual:

1) Expected Node Name vs. Actual Node Name
2) Expected Node Value vs. Actual Node Value
3) Expected Self-Closing Empty Node vs. Actual Empty Node
4) Expected Empty Node vs. Actual Self-Closing Empty Node

It's worked exceptionally well. In addition, I've written a function to split my very long (necessarily so) and complex regular expressions so that I can tell if the regular expression is matching the data properly.

Combine this with a function that uses OleDbConnection object using Jet4.0 to read test data from Excel into a dataset and you have a beautiful thing. By iterating through the rows in the dataset (I have a spreadsheet for each function I want to test and each spreadsheet has quite a few variations of test data), I can test each of the functions I have with multiple variations (literally at the click of a button).

Developers are such optimistic creatures ... with test driven development, developers can have a reason for optimism.


todd said...

I find testing makes me less optimitic

bcwhite said...

I don't really do test-driven development (where you write your tests first) but automated unit testing (or built-in self-tests, as I called them there) is a fantastic tool. It may or may not save time during the initial development, but it saves huge amounts of time during maintenence and refactoring.