Current Entries | Archives   RSS

 


Test Driven Development - I'm Sold

Thursday 23rd November, 2006


Unit Testing - and Test Driven Development - are rapidly emerging as one of the most effective techniques to improve code quality and reduce defect counts. or a while now it has been clear to me that this was the way to go to improve the quality of code - whatever framework or language was being used.

Our add-in code is native C++, but most of the Windows related resources on the subject focus on testing of .NET languages - usually C#. NUnit is well established as the leading unit testing framework for .NET, and does its job well. Not only that, but it can be closely integrated into Visual Studio via the TestDriven.NET add-in.

In contrast, the frameworks available for native code testing (notably CPPUnit) are less visible, and messier to use - CPPUnit also seems to lack a good interactive test runner. Although none of this is insurmountable, given the current heavy push on migration to managed code by Microsoft adopting a native-only single language unit test framework seems to me to be a short sighted approach.

One alternative which has seemed plausible to me for some time was to take advantage of the fact that C++ (unique of the Visual Studio languages) can be compiled to either native or managed code, and write NUnit style unit tests in Managed C++ or C++/CLI. This is something I've not seen discussed in any detail on the web at all, and unfortunately we didn't have time to investigate until recently.

However, with the release of Visual Lint 1.5 out of the way I've now had a little time to look into this again. In doing so, I have realised that the NUnit installation already includes a sample Managed C++ project (it's called cpp-sample) which forms an ideal starting point from which to experiment in this area.

Having spent around a week doing so (using Kevin Lussier's CSplitPath class and some of our utility functions as simple test subjects) I can confidently say that not only is this a viable approach to unit testing native C++ projects, but that I'm now totally sold on the concept of Test Driven Development itself.

Here's one of our sample tests:

void SplitPathTests::GetDirectory()
{
    CString sPathName = _T("C:\\Program Files\\Microsoft Visual Studio 8\\VC\\atlmfc\\include\\atlbase.h");

    CSplitPath split(sPathName);

    String* result(split.GetDirectory() );
    String* expected(S"\\Program Files\\Microsoft Visual Studio 8\\VC\\atlmfc\\include\\");

    Assert::AreEqual(expected, result);
}

Even in that small sample of test subjects, we discovered around half a dozen bugs - one of which was a serious buffer overflow in CSplitPath itself (clue: we build in Unicode, and it wasn't written that way).

In fact, I'm so convinced of the concept that we have now integrated a Unit Test project into Visual Lint itself - and one of our current tasks is being run as far as possible under TDD. In doing so, we're finding the books Unit Test Frameworks by Peter Hamill and Working Effectively with Legacy Code by Michael Feathers to be invaluable. Highly recommended.

Convinced? I certainly am.

Posted by Anna at 12:29pm | Get Permalink