1
votes

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

1
"Managed Unit Test" does not mean what just about anybody think it might mean. The project type was intended to unit test unmanaged C++ code. For some reason being able to write the unit test code in C++/CLI was considered useful, probably because of the ability to use [attributes]. I think it can be cheated, but you'd have to edit the project file by hand to add the <Reference> element. To test a C++/CLI assembly you need to use a C# or VB.NET unit test project.Hans Passant
@HansPassant You can also write them in C++/CLI although you seem to be limited to the Visual Studio Unit Test Framework and it's a round-about process to setup the project.rjzii

1 Answers

2
votes

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.