Hugo Häggmark

Sagas from the software trenches in the continuing battle between quality, price and time…

15 April 2011

Build recipe for running unit tests using file masks in TFS Build 2008

by Hugo Häggmark

Background

The background for this build script recipe can be found here and here. Instructions on how to install and configure a build server to run these build scripts can be found here and here.

The Challenge

The build recipe has to conform to these constraints/requirements:

The Dev.CI.testsettings file

As you might already know the testsettings file (formely know as testrunconfig) defines important configuration that tells MSTest various information; where to look for deployment items for instance.

I create one testsettings file per build that I’ve safely in source control and in that file I add this:

<Deployment>
  <DeploymentItem filename=\\bldserv\Bld\1\TestResults\DeplymentItems\/>
</Deployment>

These lines tells MSTest to look for deployment items in the specified directory/share. This works for now as we only have one build server that executes the CI-builds but when we add build servers later on we’ll have to come up with a better solution using environment variables for instance.

It would be great if TFS Build could automatically pick up the testsettings file for each solution in the build but as we only do ONE testrun per TFS Build that wouldn’t work either. The best would be if we ran MSTest / solution in the TFS Build.

The TFSBuild.proj file

Under the ItemGroup for testing I add the following line to tell MSTest to look for testcontainers with the “.Tests.dll” filemask.

<TestContainer Include="$(OutDir)\%2a.Tests.dll" />

The next step is to introduce a TFS Build override where we specify our testsettings file and do the copying to our deploymentitems folder specified by our testsettings file. There is also delete statement that I was added later because I found an assembly with the correct file mask but that wasn’t a unit test assembly, read more about that adventure here.

<Target Name="BeforeTestConfiguration">
  <!–We need to set the path to our xml-files and schema-files for MSTest to include this can only be done from one single testsettings-file.–>
  <PropertyGroup>
    <RunConfigFile>$(SolutionRoot)Dev.CI.testsettings</RunConfigFile>
  </PropertyGroup>

  <!–Copy all testinstance-files.–>
  <CreateItem Include="$(SolutionRoot)\**\TestInstances\*">
    <Output TaskParameter ="Include" ItemName ="MyTestFiles"/>
  </CreateItem>
  <Copy SourceFiles="@(MyTestFiles)"
        DestinationFolder="$(TestResultsRoot)\DeplymentItems"/>

  <!–Copy all schema-files.–>
  <CreateItem Include="$(SolutionRoot)\**\*.xsd">
    <Output TaskParameter ="Include" ItemName ="MySchemaFiles"/>
  </CreateItem>
  <Copy SourceFiles="@(MySchemaFiles)"
        DestinationFolder="$(TestResultsRoot)\DeplymentItems"/>

  <!–Delete all non unittest-files.–>
  <CreateItem
    Include="$(BinariesRoot)\**\[Customer].[BtsApp].OrchestrationsHelper.Tests.dll">
    <Output TaskParameter ="Include" ItemName ="MyNonUnitTestFiles"/>
  </CreateItem>
  <Delete Files="@(MyNonUnitTestFiles)"/>
</Target>

The drawback to this approach is that your testinstance/schema files (often xml-files in BizTalk projects) need to be uniquely named or you would overwrite them in this step.

Lessons learned

Please let me know what you think of this solution considering the constraints and requirements at hand. The examples for this post can be found in TFSBuild.zip.

Cheers!

Hugo

tags: BizTalk - Resolution - Team Build - Unit Testing