The Only Excuse for skipping TDD

Misko Hevery said in one of his excellent Google Clean Code Talks – the only valid excuse for not doing unit testing is not knowing how to write testable code.

Some time ago I wrote a Windows service that handled some XML imports. The whole thing was built using Reactive Extensions (Rx) and LINQ-to-XML for parsing.

Another developer had since taken over the maintenance of the project for the last few months but I had some more work to do on it today. Lo and behold my shiny unit test suite (36 tests, not something to write home about but it wasn’t all that complex) had less than 10 tests passing. In maintenance there had been a total disregard for updating the existing tests as requirements had changed.

Horrified – particularly since you can’t just hit F5 to load up a Windows service – I started looking through the failing tests. There hadn’t been all that many changes, a couple of validation checks here and there now throwing FormatExceptions and a new XML tag had been added to the XSD and the parse tree. It took a whole of about 20 minutes to get the test suite back to a passing state – meeting the requirements of the code that had changed including Asserting the correct FormatExceptions were being thrown.

Just another case where Misko was right.