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 TestResultsIn 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.
 1<!--   
 2 TESTING   Set this flag to enable/disable running tests as a post-compilation build step.   
 3\-->    
 4<RunTest\>true</RunTest\>    
 5<!--   
 6 CODE ANALYSIS   Set this property to enable/disable running code analysis. Valid values for this property are  
 7 Default, Always and Never.  
 8 Default - Perform code analysis as per the individual project settings  
 9 Always  - Always perform code analysis irrespective of project settings   
10 Never   - Never perform code analysis irrespective of project settings   
11\-->    
12<RunCodeAnalysis\>Default</RunCodeAnalysis\>  
13  
14<!--   
15 CODE COVERAGE Set the test run configuration   
16\-->    
17<RunConfigFile\>$(SolutionRoot)MyWebSolutionTestRunConfig1.testrunconfig</RunConfigFile\>
  • 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 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
 1<?xml version\="1.0" encoding\="UTF-8"?\>  
 2<TestRunConfiguration name\="TestRunConfig1" id\="b6360bec-8278-4773-a931-f22bfab2c57f" xmlns\="http://microsoft.com/schemas/VisualStudio/TeamTest/2006"\>  
 3  <Description\>This is a default test run configuration for a local test run.</Description\>  
 4  <CodeCoverage enabled\="true" keyFile\="MyWebsiteProjectKey.snk"\>  
 5    <AspNet\>  
 6      <AspNetCodeCoverageItem id\="88655819-3261-43ac-b2d8-2d3aa1aabaef" name\="MyWebsite" applicationRoot\="/" url\="http://localhost:0/" />  
 7    </AspNet\>  
 8    <Regular\>  
 9      <CodeCoverageItem binaryFile\="C:buildsMyWebsiteCIBuildBinariesRelease\_PublishedWebsitesMyWebsitebinMyWebsite.dll" pdbFile\="C:buildsMyWebsiteCIBuildBinariesRelease\_PublishedWebsitesMyWebsitebinMyWebsite.pdb" instrumentInPlace\="true" />  
10    </Regular\>  
11  </CodeCoverage\>  
12  <TestTypeSpecific\>  
13    <WebTestRunConfiguration testTypeId\="4e7599fa-5ecb-43e9-a887-cd63cf72d207"\>  
14      <Browser name\="Internet Explorer 7.0"\>  
15        <Headers\>  
16          <Header name\="User-Agent" value\="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)" />  
17          <Header name\="Accept" value\="\*/\*" />  
18          <Header name\="Accept-Language" value\="{{$IEAcceptLanguage}}" />  
19          <Header name\="Accept-Encoding" value\="GZIP" />  
20        </Headers\>  
21      </Browser\>  
22      <Network Name\="LAN" BandwidthInKbps\="0" />  
23    </WebTestRunConfiguration\>  
24  </TestTypeSpecific\>  
25</TestRunConfiguration\>
  • 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.