开始用C++/CLI做项目,但默认生成的UnitTest在项目中使用了C++代码时会出错。这个问题在困扰了我两天后,终于找到了答案,共享之。

    Re: Testing Native Code (C++ ) using Team Unit Testing frameworks 
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=145950&SiteID=1
    注:写这个回复的Michael Koltachev是 微软 Visual Studio Team System的一个开发者
    
......

    Also let me do some clarification on original question about our support for testing native code:
- We support running tests native/managed dll/exe compiled with /clr (any of: /clr, /clr:safe, /clr:pure, etc).
- We support generating tests for C++ code only if the dll/exe is compiled with /clr:safe

So to test native code the best option is to:
- Create an empty C++ Test Project, then open Project->Properties->General and change "Common Language Runtime support" from /clr:safe to /clr. Then you can write Unit Tests by hand as: [TestClass] public ref class TestClass {public: [TestMethod] void Test() {} }; You can use full power of unmanaged C++, call your native code directly, etc inside your Unit Tests.

The are other options but I would say you don't want to do that:
- Write managed C++ wrapper and auto-generate Unit Tests for the managed C++ wrapper.
- Create new VB, C# or MC++ Test Project and call your unmanaged code from test methods using [DllImport].
- If your C++ code is COM typelib, you could register the lib, and could add the lib to your test project's references (via COM tab in Add references dialog). Then you can exercise your C++ code in managed test code.

Thanks,
Michael