Search code examples
visual-studio-2015c++-clivs-unit-testing-framework

Adding Managed Test Project to existing C++/CLI Solution


I'm in the process of working on a legacy project that was written in C++/CLI as a Windows Application with everything being compiled into a single .exe file. Since I've been tasked with writing some mathematically complex code I want to add unit testing to the solution. Upon doing so (Solution > Add > New Project > Managed Test Project) and attempting to add the project as a reference to the test project I get the following error:

Visual Studio Error Message

The main project is configured as "Pure MSIL Common Language Runtime support (/clr:pure)" and everything obvious seems to be in order, but I keep getting the error message. How can I get the solution setup to include the Unit Testing Framework?

Google Search Phrase: Add Unit Tests to C++/CLI Project


Solution

  • As it turns out and was eluded to by a commenter, you can't use the C++ Test Projects when writing tests for C++/CLR. The workaround that we found is as follows:

    1. Create a new project via "[Solution] > Add > New Project > Visual C++ > CLR > CLR Empty Project"
    2. Under "[Project] > Properties" set the "Configuration Type" to "Dynamical Library (.dll)"
    3. Add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll ours is version 14.0.23107.0
    4. Add a reference to the solution project you wish to test
    5. Start writing the relevant unit tests being sure to not #include headers from the other project, Visual Studio will resolve them for you
    6. Build the solution [Ctrl+Shift+B]
    7. Run your unit tests through the built in Test Explorer [Ctr+R, A]

    We also verified that the tests can be run through the Unit Test Session in ReSharper 10.0.2.