Testing Native Code (C++ ) using Team Unit Testing frameworks
- I have been looking for some pointers or guide lines as to how do I test C++ unmanaged code using Unit Tesing capabilities of VS 2005.
Do I need to write wrappers in managed code ( a sort of mixed mode class one that calls into managed code and other that calls the native code)
Any suggestion welcome
Regds
Dinesh
Answers
- 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.
If your C++ code is not COM lib, you can use DllImport to import the functions and exercise them in managed test code.
Hope this helps.
Leo Huang
Dev, VSTS There are a few options:
- Create new Managed C++ Test Project. Change project options general->CLR support from /clr:safe to /clr. Call code under test C++ code in regular C++ way. This is probably the easiest way
- Write managed C++ wrapper and auto-generate Unit Tests. VSTS Unit Test generation wizard does not generate code for unmanaged C++ but it does for managed C++.
- Create new VB, C# or MC++ Test Project and call your unmanaged code directly from test methods using [DllImport] or COM as Leo proposed.Thanks,
Michael
All Replies
- 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.
If your C++ code is not COM lib, you can use DllImport to import the functions and exercise them in managed test code.
Hope this helps.
Leo Huang
Dev, VSTS There are a few options:
- Create new Managed C++ Test Project. Change project options general->CLR support from /clr:safe to /clr. Call code under test C++ code in regular C++ way. This is probably the easiest way
- Write managed C++ wrapper and auto-generate Unit Tests. VSTS Unit Test generation wizard does not generate code for unmanaged C++ but it does for managed C++.
- Create new VB, C# or MC++ Test Project and call your unmanaged code directly from test methods using [DllImport] or COM as Leo proposed.Thanks,
Michael- Hi!
Ich have the same problem, some unmanaged C++ code which I would like to test.
How do I "register the lib"?
Thanks
Use regsvr32.exe. You can only do that when it is a COM type library.
Thanks,
Leo Huang
Dev, VSTSThanks,
Michael
I tried with your first approach. What is the way to validate the test case. every time I am getting passes.
my function is
HRESULT ReplaceString(__in LPWSTR wzinStr, __in LPWSTR wzreplaceFor,__in LPWSTR wzreplaceBy, __out LPWSTR *pwzoutStr)my test code in framework looks like
[TestMethod]
void TestMethod1()
{
WCHAR wzfinalPath[_MAX_MYFILE];
WCHAR wzpath[] = L
"\\\\%temp%\\\\abc\\\\Abadt.xml\\\\\\\\";LPWSTR ppoutStr = NULL;
HRESULT hr = ReplaceString(wzpath,L
"\\\\",L"\\",&ppoutStr);};
now i want to validate hr with S_OK and ppoutStr with "\\%temp%\\abc\\Abadt.xml\\\\".
please answer how will I do the validation through framework so that when these two validatios pass my test case will be passed.
TestMethod1() does not return anything, how framework decides pass/fail of a test case ?
Thanks,
Paresh- Paresh, Unit Test Framework allows you to do validation using Assert class, something like:
Assert::AreEqual(S_OK, hr);
Assert::AreEqual("validation string", gcnew System::String(ppoutStr));
You can also do Assert::AreEqual(0, wcscmp(L"verification string")); but in this case error message will not include the actual string.
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 - Thanks Michael,
I did with the option Common Language Runtime support" to /clr. it is working fine. but I could not get a way to decide the pass/fail condition.
In managed envirionment Assert.isTrue() is there to decide the Pass/fail condition.
What is the similar method in unmanaged test ?
could you please suggest me suitable procedure for this.
Thanks,
Paresh - Paresh, please see my post above. Let me know if you have other issues.
Thanks,
Michael - Thanks,
Michael
It solved the problem. and sorry for missing the first paragraph.
Thanks
Paresh Hi,
I can't make use of the ExpectedExceptionAttribute as it expect a type that derives from Exception managed type.Is there any work around to test a native exception without wrapping the whole native type including the native exception itself?
Thaknks.I don't see any workaround for this. For ExpectedExeption attribute you can specify either managed exception type or type + error message. For unmanaged exception you would get something like System.Runtime.InteropServices.SEHException. If you need to verify what's inside and the test in in C++, don't use ExpectedException, just do try-catch on the entire test body and verify exception yourself.
Thanks,
Michael


