FANDOM


Unit Testing ToolEdit

Whether MSTest / VSTS or NUnit is used as the unit testing tool, both require that test code reside in a .NET assembly. The following are the methods by which either tool can be used to test unmanaged C++ code.

Testing COM objectsEdit

If the C++ production code is already a COM object, then you simply create a interop assembly for the COM object, and then write your unit tests in C# or C++/CLR that call the COM object methods

Examples and Additional details <-- enter more details here.

Testing Standard DLLsEdit

If the C or C++ production code is in a standard DLL, with function exports, then you have two options:

  1. Write your unit tests in C# or C++/CLR, and use P/Invoke to call the DLL functions.
  2. Write your unit tests in C++/CLR, and use C++ Interop (Implicit PInvoke)

Examples and Additional details <-- enter more details here.

Testing C++ classes using #includeEdit

If your C++ classes are self contained, then you can write your unit tests in C++/CLR, and directly #include the C++ classes into your unit test code. Note that in this case, the C++ code is actually compiled as managed code.

Examples and Additional details <-- enter more details here.

Testing C++ classes using exported classesEdit

If your C++ production code is in a DLL, you can export the C++ classes using __declspec(dllexport), then write your unit test using C++/CLR, link to the import library of your production DLL, and call the C++ class methods as usual.

It's possible that the instantiating a production C++ class from the unit test will not work because of complex header and template dependencies that cannot be easily resolved. The workaround is to add a standard exported function to the production DLL which instantiate the C++ class and returns an instance of it to the unit test. You still need to use __declspec(dllexport) on the C++ class, however, as well as on the exported function.

Examples and Additional details <-- enter more details here.

Testing EXEsEdit

You can't call functions in an EXE from a Test DLL. You'll have to create another project to compile the code into a DLL and then use one of the options above.

Examples and Additional details <-- enter more details here.

Testing Static LibrariesEdit

If the static-library is self-contained, you can write your unit test in C++/CLR and link the static library to the unit test directly. But if it's not self-contained, you'll get linker errors, which means the library requires other code that probably resides in another library or in the main program. You'll need to resolve these, which you may be able to by adding those other libraries to your project. Another way is to declare the unresolved symbols in the unit test so that the linker resolves to them.

If it's possible to recompile the static-library into a DLL, that would be the preferred solution because then you can use one of the options above that work with DLLs. Even if the production build requires the code to be delivered as a static-library, it may be possible to create another project that compiles the same code into a DLL, for testing purposes only.

Examples and Additional details <-- enter more details here.

ReferencesEdit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.