Blog Archives

Setting the Test Run Config in Team Build

At my current client, we’re in a situation where a couple of us have Visual Studio 2008 Team System and the rest have Professional Edition. This means that we’ve been having a hard time with getting Code Coverage in our team build because everyone has been changing the active test configuration to suite their environment.

After trawling through a few blog posts and support forums, I finally discovered a gold nugget. In a couple of steps, I defined the test configuration and get it to run as part of the team build.

Firtsly, I added the following test arguments to my project build file (TFSBuild.proj):

<MetaDataFile Include="$(SolutionRoot)/HelloWorld.vsmdi">
  <TestList>HelloWorldUnitTests</TestList>
  <RunConfigFile>$(SolutionRoot)/TeamBuildTestRun.testrunconfig</RunConfigFile>
</MetaDataFile>

This defines the test metadata file (HelloWorld.vsmdi), the list of tests to execute (HelloWorldUnitTests) and the configuration file to use (TeamBuildTestRun.testrunconfig). However, only the metadata file and test list will be included to the MSTest command. To get it all working, we have to edit the targets file on the server C:\Program Files\MSBuild\Microsoft\VisualStudio\Team\Microsoft.TeamFoundation.Build.targets. There is a target called CoreTestConfiguration that calls the TestToolsTask MSBuild task with the parameters. The first three calls are for non-desktop (i.e. server) builds, e.g.

<TestToolsTask
      Condition=" '$(IsDesktopBuild)'!='true'
                  and '$(V8TestToolsTask)'!='true'
                  and '%(LocalMetaDataFile.Identity)' != '' "
      BuildFlavor="$(Configuration)"
      Platform="$(Platform)"
      PublishServer="$(TeamFoundationServerUrl)"
      PublishBuild="$(BuildNumber)"
      SearchPathRoot="$(OutDir)"
      PathToResultsFilesRoot="$(TestResultsRoot)"
      MetaDataFile="%(LocalMetaDataFile.Identity)"
      RunConfigFile="%(RunConfigFile)"
      TestLists="%(LocalMetaDataFile.TestList)"
      TeamProject="$(TeamProject)"
      TestNames="$(TestNames)"
      ContinueOnError="true" />

When the build is run on the server, the values of the MetaDataFile property are copied to the LocalMetaDataFile variable. This means the RunConfigFile property needs to be changed to %(LocalMetaDataFile.RunConfigFile), e.g.

<TestToolsTask
      Condition=" '$(IsDesktopBuild)'!='true'
                  and '$(V8TestToolsTask)'!='true'
                  and '%(LocalMetaDataFile.Identity)' != '' "
      BuildFlavor="$(Configuration)"
      Platform="$(Platform)"
      PublishServer="$(TeamFoundationServerUrl)"
      PublishBuild="$(BuildNumber)"
      SearchPathRoot="$(OutDir)"
      PathToResultsFilesRoot="$(TestResultsRoot)"
      MetaDataFile="%(LocalMetaDataFile.Identity)"
      RunConfigFile="%(LocalMetaDataFile.RunConfigFile)"
      TestLists="%(LocalMetaDataFile.TestList)"
      TeamProject="$(TeamProject)"
      TestNames="$(TestNames)"
      ContinueOnError="true" />

There are three more calls to TestToolsTask that should be modified. These calls are for desktop builds, so the LocalMetaDataFile has not been created. This means we use %(MetaDataFile.RunConfigFile) instead, e.g.

  <TestToolsTask
        Condition=" '$(IsDesktopBuild)'=='true'
                    and '$(V8TestToolsTask)'!='true'
                    and '%(MetaDataFile.Identity)' != '' "
        SearchPathRoot="$(OutDir)"
        PathToResultsFilesRoot="$(TestResultsRoot)"
        MetaDataFile="%(MetaDataFile.Identity)"
        RunConfigFile="%(MetaDataFile.RunConfigFile)"
        TestLists="%(MetaDataFile.TestList)"
        TestNames="$(TestNames)"
        ContinueOnError="true" />

And that’s it!