If you've ever tried to run unit tests for C++, the landscape is much less appealing. C++ does not have a reflection API, nor does it have attributes that are embedded in the executable code, so it's much more tedious in C++ to do all of the housekeeping to initialize the framework and it's much more difficult to integrate external GUI tools. In short, unit test in C++ is a sub-par experience compared to more modern languages.
But I have good news for you - it's possible to use NUnit with C++. There are two minor caveats:
- You must be using Microsoft Visual Studio 2005 or later (sorry MinGW and cygwin users.)
- The code to be tested must either be a DLL or a static library. If you need to test code in an .EXE, you should factor that code out into its own static library.
If you haven't built this kind of project before, a C++/CLI project is a curious hybrid that contains all of the power of C++ as well as much of the power of .Net. (Access to certain features, like LINQ, is not available in C++/CLI.)
With a C++/CLI project, you can use NUnit attributes for classes and member functions, just like in C#. When you run NUnit, your target is your test DLL, which implicitly loads either your static library code or your DLL code.
This strategy can also be used in the test environment build into Visual Studio if you have the Professional edition or better.
I have two unmanaged c++ projects in vs2008. One produce a dll, and one produce an exe (command line tool).
ReplyDeleteHow would you setup a clr dll to test functionality in these projects with NUnit?
There's an article about it on CodeProject (not by me):
ReplyDeletehttp://www.codeproject.com/KB/cpp/NUnitNativeCPP.aspx
The explanation is a little terse, but there's some sample code to download that should get you started.
You cannot use nUnit to test an EXE. The strategy I used was to put the non-UI code in a .lib file (static linking, not DLL), then link that directly to the CLR test DLL for nUnit.