One of the challanges that I have been having lately was getting code coverage working on our build machine.  I was able to get code coverage working for normal dll’s but I couldn’t get it working for Web Application projects.    The old saying “it worked on my machine” held true in this case.    I could get the code coverage working for the Web application project, but I couldn’t get it working on my machine.   After scratching my head for a while, I thought to myself where is the code for the web site getting executed when you just tests against the web site.    After having a look on the build machine, I had a hunch that it might be in the _PublishedWebSites directory.   So I added a quick bit of code to the web application project to write out it’s location to the event log and it confirmed my hunch.   (For those of you who are unsure where the _PublishedWebSite directory is found it is in <BuildDirectory>\<TeamProjectName>\<BuildTypeName>\Binaries\<Configuration>\_PublishedWebSites.  On my build machine that is d:\build\TestTeamProject\TestContinuous\Release\_PublishWebSites.    

So how did I solve the problem.   What I did was open up the solution file on the build machine underneath the  <BuildDirectory>\<TeamProjectName>\<BuildTypeName>\Source directory.   I then openned up the .testrunconfig file (See Enabling Code Coverage) and added the assemblies that I wanted to enable code coverage for and I suddenly had code coverage results.

 

For those who don’t have access to the build machine, you can achieve the same thing by editing the .testrunconfig file manually.    Instead of double clicking on the .testrunconfig file, right mouse click and selected open with.  This will bring up the following dialogue. 

Select the XML Editor.  This will bring up an XML editor from which you can manual edit the file.   The section that you are looking for is below:

 

<isCodeCoverageEnabled type="System.Boolean">True</isCodeCoverageEnabled>
    <codeCoverageItems type="System.Collections.Generic.List`1[[Microsoft.VisualStudio.TestTools.Common.CodeCoverageItem, Microsoft.VisualStudio.QualityTools.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]">
     <_items type="Microsoft.VisualStudio.TestTools.Common.CodeCoverageItem[]">
        <element type="Microsoft.VisualStudio.TestTools.Common.CodeCoverageItem">
         <binaryFile type="System.String">bin\Debug\UpdateWarehouse.exe</binaryFile>
         <keyFile type="System.String" />
         <pdbFile type="System.String">obj\Debug\UpdateWarehouse.pdb</pdbFile>
         <instrumentInPlace type="System.Boolean">True</instrumentInPlace>
         <outputDirectory type="System.String" />
        </element>
     </_items>
     <_size type="System.Int32">1</_size>
     <_version type="System.Int32">1</_version>
    </codeCoverageItems>

All that you need to do is add in one of the elements and update the _size and _version with the number of elements in the collection array.      A tip that I discovered.  Normally when you open the .testrunconfig file as an XML document all you see if one long string.   If you do CTRL+A then CTRL+F (That is select all then Edit.FormatDocument). 

Advertisements