WinUnit: An Outstanding Native C++ Unit Testing Tool

25 Comments January 17, 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.


25 Comments

  • Gravatar Image
    Kenny Kerr January 17, 2008 4:43 PM

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

  • Gravatar Image
    Sarath January 17, 2008 9:18 PM

    From Where I can download WinUnit?

  • Gravatar Image
    jrobbins January 17, 2008 10:40 PM

    Sarath,

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

    - John Robbins

  • Gravatar Image
    Sarath January 20, 2008 1:54 AM

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

  • Gravatar Image
    Karl-Heinz January 22, 2008 12:48 PM

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

  • Gravatar Image
    Mike Miller January 29, 2008 1:24 PM

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

  • Gravatar Image
    jrobbins February 6, 2008 12:58 AM

    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

  • Gravatar Image
    Karl-Heinz February 27, 2008 6:50 AM

    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

  • Gravatar Image
    Manfred Wilner April 30, 2008 10:59 AM

    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

  • Gravatar Image
    jrobbins April 30, 2008 7:35 PM

    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

  • Gravatar Image
    Manfred Wilner April 30, 2008 10:21 PM

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

    \Manfred

  • Gravatar Image
    Manfred Wilner April 30, 2008 10:25 PM

    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

  • Gravatar Image
    jrobbins April 30, 2008 10:25 PM

    Manfred,

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

    - John Robbins

  • Gravatar Image
    Sonia June 16, 2008 4:56 AM

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

  • Gravatar Image
    jrobbins June 16, 2008 9:46 AM

    Sonia,

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

    - John Robbins

  • Gravatar Image
    Bin July 2, 2008 3:20 AM

    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?

  • Gravatar Image
    jrobbins July 2, 2008 10:27 AM

    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.

  • Gravatar Image
    C++ Testing Tools November 4, 2009 8:39 AM

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

  • Gravatar Image
    jrobbins November 4, 2009 10:59 AM

    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

  • Gravatar Image
    Roma December 17, 2009 9:39 PM

    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

  • Gravatar Image
    Alex April 15, 2010 9:40 AM

    "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.

  • Gravatar Image
    jrobbins April 15, 2010 11:15 AM

    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

  • Gravatar Image
    Fred September 13, 2011 6:32 AM

    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.

  • Gravatar Image
    jrobbins September 13, 2011 2:43 PM

    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

  • Gravatar Image
    Fred September 15, 2011 4:21 AM

    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.

Have a Comment?

Archives

Blogs