views:

891

answers:

1

I'm trying to set up unit testing with code coverage in VS2008, for a C++/CLI DLL which is compiled with /clr (not /clr:safe or /clr:pure - it has to be /clr because it uses MFC).

The unit tests work perfectly but the coverage information only works if I compile with /clr:safe or /clr:pure. For /clr the Code Coverage Results window shows the following message:

Empty results generated: none of the instrumented binary was used. Look at test run details for any instrumentation problems.

I've also tried "going offroad" but when I load the coverage file into VS, it also contains empty results.

Annoyingly I can't find anywhere that specifically says whether Code Coverage works with /CLR, so I just had to try it myself.

If it should work, can anyone see what I'm doing wrong here?


[File]->[New]->[Project]
Select Class Library, enter MyProj as the project name, click OK

Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Ensure "Common Language Runtime support" is set to /CLR

Add this code to Class1:

public:
 static int calc() { return 69; }

Build solution

[Test]->[New Test]->[Unit Test], click OK, click Create

Add this code to TestMethod1:

Assert::AreEqual(MyProj::Class1::calc(), 69);

Right-click on TestProject1 project, select [References]
Click "Add New Reference"
Select MyProj in the "Projects" tab, click OK, click OK again

[Test]->[Edit Test Run Configuration]->[Local Test Run]
Select [Code Coverage]
Check MyProj.dll, click Apply, click Close

[Test]->[Run]->[All Tests in Solution]

The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window shows the following message:

Empty results generated: none of the instrumented binary was used. Look at test run details for any instrumentation problems.


Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Change "Common Language Runtime support" to /CLR:SAFE or /CLR:PURE, click OK

Build solution

[Test]->[Run]->[All Tests In Solution]

The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window now shows correct coverage information.

A: 

Based on http://msdn.microsoft.com/en-us/library/ms182534.aspx

1-project must be in debug

2.-in the project properties you must select x 86 platform.

3 Unregister the project being tested in the GAC.

Jaimir Guerrero MVP