MSDN Magazine - February 2008 - (Page 74) Maria Blees WinUnit Simplified Unit Testing For Native C++ Applications hese days it can be hard not to feel downright oppressed as a native code developer—it seems like the developers using the Microsoft® .NET Framework get all the cool tools! I’ve always been interested in good engineering practices, but I’ve been frustrated by so-called “engineering experts” who extol the virtues of unit testing, yet can offer little more than hand waving when asked for tool recommendations for native code. What I really wanted, and what I thought would be the easiest to integrate into an automated build system, was the equivalent of NUnit for native code. That is, I wanted to be able to make a DLL with only tests in it, and have an external test-runner that would run those tests and take care of the reporting and logging. I also wanted to be able to declare each test only once, and to have a minimum of extra code in my test DLL. So I built a native code unit testing tool I call WinUnit. I’ll go into more detail later, but here’s a little preview of how easy it is to create and run a test DLL using WinUnit. To start, create a single CPP file—let’s call it DummyTest.cpp: #include “WinUnit.h” BEGIN_TEST(DummyTest) { WIN_ASSERT_TRUE(3 > 4, _T(“You should see this error message.”)); } END_TEST cl /EHsc /LD DummyTest.cpp Then execute WinUnit on it: >WinUnit DummyTest.dll Processing [DummyTest.dll] (DummyTest) DummyTest.cpp(5): error : WIN_ASSERT_TRUE failed: “3 > 4”. You should see this error message. FAILED: DummyTest. [DummyTest.dll] FAILED. Tests run: 1; Failures: 1. There were errors. Tests run: 1; Failed: 1. Changing 3 > 4 to the true expression 4 > 3 will of course get rid of the failure. This article uses the following technologies: C++, Visual Studio This article discusses: Unit testing philosophy Setting up WinUnit ✥ Building test fixtures ✥ Implementation and customization ✥ ✥ Code download available at: msdn.microsoft.com/msdnmag/code08.aspx Maria Blees has been a developer at Microsoft for 10 years and has a special fondness for native code and engineering excellence. She can be reached at the address listed in the code and welcomes bug reports and suggestions for this tool. Build it into a DLL with this command: 74 msdnmagazine http://msdn.microsoft.com/msdnmag/code08.aspx
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.