Wintellect Blogs

WinUnit: An Outstanding Native C++ Unit Testing Tool

17 Jan , 2008  

Whew! I can finally talk about one of the coolest pieces of code I’ve seen in ages: WinUnit. About a year ago, my friend, Maria Blees showed me an idea she had for unit testing native C++ code and asked if I thought it was any good. It was far more than good it was outstanding! WinUnit brings NUnit-like testing to native C++ code. What makes WinUnit very nice is that writing tests is easy, but what makes it truly great is it includes the infrastructure to run those tests. That’s what’s been missing from other native testing approaches.

I ported over a good size library I had, which used separate EXE files for testing to use WinUnit. It turned out that I cut the code down by 40% and got 40% more automated code coverage! That’s one heck of a return on your investment.

Maria’s article in MSDN Magazine discusses all the ins and outs of using WinUnit so you can see how to apply it immediately to your code. She even included a set of Visual Studio macros to configure your projects to use WinUnit. Most impressively, she included all her unit tests, written in WinUnit of course.

The most common question I get for native development is “how can I unit test C++?” Now, you have the answer. It’s been killing me to have to hold WinUnit secret until it finally was published in MSDN Magazine. It’s not too often I’m stunned speechless a piece of code any more, but WinUnit was one of those. I can’t recommend WinUnit enough!

Update April 30, 2008: MSDN Magazine broke their years of links recently so I fixed the links.


  • http://weblogs.asp.net/kennykerr/archive/2008/01/17/unit-testing-for-native-c.aspx Anonymous

    I often get asked about unit testing native code and quite frankly I’ve never really had a good answer

  • http://sarathc.wordpress.com Anonymous

    From Where I can download WinUnit?

  • jrobbins

    Sarath,

    See the MSDN Magazine article at http://msdn.microsoft.com/msdnmag/issues/08/02/NativeUnitTesting/default.aspx.

    – John Robbins

  • http://Sarathc.wordpress.com Anonymous

    John Sir,
    Thanks. I never looked into the source code.

  • Anonymous

    WinUnit reminds me of unittest++, which is by the way plattform-independent. Why inventing the wheel twice?

  • Anonymous

    This looks very similar to CPPUnit. It would be great to find a comparision of the various C++ options for unit test frameworks.

  • jrobbins

    Karl-Heinz and Mike Miller

    The problem with unittest++ and CPPUnit is they are missing a fundimental piece: a simple to use infrastructure for running tests. With WinUnit, you can run all tests, one test, or any combination of tests. The other frameworks are run all your tests all the time or you get the pain of writing your own runner.

    WinUnit is the only tool that truly brings NUnit like functionality to native Windows developers. That’s why I like it so much.

    – John Robbins

  • Anonymous

    You are right, the TestRunner is missing in other frameworks as those do not depend on a plattform nor on a certain developer-environment. BTW unittest++ tests can be started in the postbuild-step of VisualStudio, errors are reported in the output pane…

    But talking “about one of the coolest pieces of code I’ve seen in ages…”… hm, sorry I can’t agree.

    @Mike Mille: have a look at: http://www.gamesfromwithin.com/articles/0412/000061.html

  • Anonymous

    John, it looks like as if the link to the code download from the February Magazine is broken. You can download the file but it will not extract and fail with an error.

    Can you provide a different link?

    Thank you,
    \Manfred

  • jrobbins

    Manfred,

    Grrrr. MSDN Magazine went off and broke 15+ years of links. I updated the links in my blog post. Also, in their infinite wisdom, MSDN Magazine no longer lets you download an individual article’s code. You have to get the full issue’s code. The Feb ’08 issue code is at: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/MSDNMag2008_02.exe

  • Anonymous

    Thank you John, this link worked and the file extracted properly.

    \Manfred

  • Anonymous

    John,
    I have another question.
    Have you used WinUnit with the WDK and printer driver?
    I am re-designing our Unidriver based driver for Vista and want to start TDD with this project.
    I had looked at CppUnitTest and NUnit but find it quite difficult to adapt this to the WDK compiler.

    Any suggestions are appreciated.

    Thank you,
    \Manfred

  • jrobbins

    Manfred,

    No problem at all. I completely and totally love WinUnit and want to get it in as many hands as possible!

    – John Robbins

  • Anonymous

    Will winunit work in VS.NET 2003 or is it designed only for Visual studio 2005 applications?

  • jrobbins

    Sonia,

    It’s compiled with VS 2005, but I don’t think there would be any problems backporting it to VS 2003.

    – John Robbins

  • Anonymous

    Dear John,

    Just wandering can we do individual test with WinUnit, e.g., how to test a single function or a single class without testing the whole production code?

  • jrobbins

    Bin,

    You’ll just write the test to execute the portion of the code you want. To run just that one test, use the -e command line switch to WinUnit.exe.

    Hope it helps!

    – John Robbins.

  • http://www.testertools.com/267-C/C++.html Anonymous

    great….good choice,,thanks for sharing. for more C++ Testing Tools, you can visit testertools.com website.

  • jrobbins

    C++ Testing Tools,

    Thanks for the link to your site. Lots of good stuff you’ve linked to consolidate the tools for everyone.

    – John Robbins

  • Anonymous

    Dear John

    I think that one of the coolest thing of WinUnit is that
    you can easily test internals of a DLL. Using other framework it is impossible to test function or class without exposing it to the DLL interface.

    Of course I can be wrong.

    Currently I’m selecting unit test framework for my project, where I intensively use DLL as plugins. WinUnit seems to fit well.

    – Roma

  • Anonymous

    “It’s compiled with VS 2005, but I don’t think there would be any problems backporting it to VS 2003.”

    There is a fairly major problem with porting it to any compiler earlier than 2005. WinUnit uses variadic macros, which were introduced in VS2005.

  • jrobbins

    Alex,

    Well, VS 2003 was released over 8 years ago so I can’t remember differences in feature sets from that long ago. :) Seriously, people really should look at moving their tool sets off 8 year old compilers.

    – John Robbins

  • Anonymous

    I’m currently using winunit on VS2010 C++, which is pretty cool.
    I`m wondering if it’s possible to set the order the tests are being called?
    I’ve realised the WinUnit framework uses the test function name in alphabetical order to run the tests, so prefxing them with 1_… 2_… can do the trick, but it can be a bit of a pain if we need to shuffle around tests.
    any idea?
    cheers.

  • jrobbins

    Fred,

    I would respectfully submit that you are doing your tests wrong. Best practices dictate that each test should be able to run by itself. You should look at test fixtures to do setup and tear down operations before and after each test.

    – John Robbins

  • Anonymous

    hi John,
    I do agree with you that unit tests should be able to run independently from each others, but I was refering more to the testInit() and testCleanup() functions that can be used to initialised a test suite and then performs a clean up when done (memory allocation, connections, etc. ), based on CUnit exp.