title: VS2008: Unit Testing with Code Coverage doesn't work with /CLR
body: <p>I'm trying to set up unit testing with code coverage in VS2008, for a C++/CLI DLL which is compiled with <strong>/clr</strong> (not /clr:safe or /clr:pure - it has to be /clr because it uses MFC).</p>
  
  <p>The unit tests work perfectly but the coverage information only works if I compile with /clr:safe or /clr:pure. For /clr the <em>Code Coverage Results</em> window shows the following message:</p>
  
  <blockquote>
    <p>Empty results generated: none of the
    instrumented binary was used. Look at
    test run details for any
    instrumentation problems.</p>
  </blockquote>
  
  <p>I've also tried "<a href="http://blogs.msdn.com/ms_joc/articles/406608.aspx" rel="nofollow noreferrer">going offroad</a>" but when I load the coverage file into VS, it also contains empty results.</p>
  
  <p>Annoyingly I can't find anywhere that specifically says whether Code Coverage works with /CLR, so I just had to try it myself.</p>
  
  <p>If it should work, can anyone see what I'm doing wrong here?</p>
  
  <hr>
  
  <p>[File]->[New]->[Project]<br>
  Select <em>Class Library</em>, enter <em>MyProj</em> as the project name, click OK</p>
  
  <p>Right-click on MyProj project, select [Properties]<br>
  Select [Configuration Properties]->[General]<br>
  Ensure "Common Language Runtime support" is set to <strong>/CLR</strong></p>
  
  <p>Add this code to <em>Class1</em>:</p>
  
  <pre><code>public:
      static int calc() { return 69; }
  </code></pre>
  
  <p>Build solution</p>
  
  <p>[Test]->[New Test]->[Unit Test], click OK, click Create</p>
  
  <p>Add this code to <em>TestMethod1</em>:</p>
  
  <pre><code>Assert::AreEqual(MyProj::Class1::calc(), 69);
  </code></pre>
  
  <p>Right-click on TestProject1 project, select [References]<br>
  Click "Add New Reference"<br>
  Select <em>MyProj</em> in the "Projects" tab, click OK, click OK again</p>
  
  <p>[Test]->[Edit Test Run Configuration]->[Local Test Run]<br>
  Select [Code Coverage]<br>
  Check <em>MyProj.dll</em>, click Apply, click Close</p>
  
  <p>[Test]->[Run]->[All Tests in Solution]</p>
  
  <p>The <em>Test Results</em> window shows TestMethod1 has passed.<br>
  The <em>Code Coverage Results</em> window shows the following message:</p>
  
  <blockquote>
    <p>Empty results generated: none of the
    instrumented binary was used. Look at
    test run details for any
    instrumentation problems.</p>
  </blockquote>
  
  <hr>
  
  <p>Right-click on MyProj project, select [Properties]<br>
  Select [Configuration Properties]->[General]<br>
  Change "Common Language Runtime support" to <strong>/CLR:SAFE</strong> or <strong>/CLR:PURE</strong>, click OK</p>
  
  <p>Build solution</p>
  
  <p>[Test]->[Run]->[All Tests In Solution]</p>
  
  <p>The <em>Test Results</em> window shows TestMethod1 has passed.<br>
  The <em>Code Coverage Results</em> window now shows correct coverage information.</p>