Team Build, Code Coverage and MVC

I have been working on some automated build and testing for a project based on the Microsoft MVC framework. The build was working fine, and test were being run, but I was not seeing any code coverage data in the build summary in Visual Studio for the builds done by the Team Build box. However if I ran the test suite locally on a development PC the coverage data was there. Looking on the Team Build drop location I could find the data.coverage file in the TestResults\<guid>\In\<build user> folder, but it was 84Kb in size, which I learnt means ‘contains no data’.

After a good deal of hunting I found a pointer to the answer the OZTFS forum. The problem is that as the MVC project is a web project, and it build a _PublishedWebsites folder and puts the assemblies into this. In effect the code coverage is just looking in the wrong place.

The fix is as follows:

  • Make sure you have suitable TestRunConfig1.testrunconfig file in your solution
  • Open this file in the VS IDE and make sure code coverage is enabled for the assemblies you want (and if your assemblies are signed that the re-sign key is set)
  • Open you tfsbuild.proj file for the automated team build and make sure you have a testing block similar to the block below, change the path to the RunConfigFile as required.

TESTING Set this flag to enable/disable running tests as a post-compilation build step.
CODE ANALYSIS Set this property to enable/disable running code analysis. Valid values for this property are
Default, Always and Never.
Default - Perform code analysis as per the individual project settings
Always - Always perform code analysis irrespective of project settings
Never - Never perform code analysis irrespective of project settings

CODE COVERAGE Set the test run configuration

  • If you test this locally you should get code coverage results, but if you run the build on a Team Build box the code coverage section in the test report will show "No coverage result"
  • Now the important bit – open the TestRunConfig1.testrunconfig file in Notepad and add an extra block to the <regular> code coverage section to additionally point to the assembly(s) in the _PublishedWebsites structure (you could also use the VS IDE on the build box to add the file if you wanted, but this will warn over an assembly being added twice). When complete the XML file should look similar to the one below
<?xml version="1.0" encoding="UTF-8"?>
<TestRunConfiguration name="TestRunConfig1" id="b6360bec-8278-4773-a931-f22bfab2c57f" xmlns="">
<Description>This is a default test run configuration for a local test run.</Description>
<CodeCoverage enabled="true" keyFile="MyWebsite\ProjectKey.snk">
<AspNetCodeCoverageItem id="88655819-3261-43ac-b2d8-2d3aa1aabaef" name="MyWebsite" applicationRoot="/" url="http://localhost:0/" />
<CodeCoverageItem binaryFile="C:\builds\MyWebsite\CIBuild\Binaries\Release\_PublishedWebsites\MyWebsite\bin\MyWebsite.dll" pdbFile="C:\builds\MyWebsite\CIBuild\Binaries\Release\_PublishedWebsites\MyWebsite\bin\MyWebsite.pdb" instrumentInPlace="true" />
<WebTestRunConfiguration testTypeId="4e7599fa-5ecb-43e9-a887-cd63cf72d207">
<Browser name="Internet Explorer 7.0">
<Header name="User-Agent" value="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)" />
<Header name="Accept" value="*/*" />
<Header name="Accept-Language" value="{{$IEAcceptLanguage}}" />
<Header name="Accept-Encoding" value="GZIP" />
<Network Name="LAN" BandwidthInKbps="0" />

  • Once this is done you can run the tests locally or on the build machine and in both cases MSTest manages to find the assembly to test the code coverage on and reports the results.