Mathias Olausson [MVP]

Automatically update test plan with build number

with 3 comments

A great feature in Microsoft Test Manager is that it allows you to track which application build your are running your tests against. This is useful for manual testing to track which tests was run against a particular build, it will be assigned to a bug reported during testing and it is used to locate test assemblies used for running automated tests.

It’s really simple to set this up, just go to the properties for the test plan and assign the build definition to the test plan:

image

Unfortunately you need to assign the Build in use manually, which is both tedious and easy to forget.

The solution to this is of course automation. The TFS 2013 build templates can easily be extended to run a post build script (typically PowerShell). See http://msdn.microsoft.com/en-us/library/vstudio/dn376353.aspx if you want to know more about how to run a script in the build process.

The script below will find all test plans in the team project where the build definition is used and update them with the latest build. All you need to do is add the script to source control and reference it in the build definition:

image

Running the build will update the test plans using the build definition to the latest build:

image

The latest version of the script is available from TFS Community Build Extensions on Github here: UpdateTestPlanBuildNumber.ps1. A snapshot is shown below:

##———————————————————————–

## <copyright file=”UpdateTestPlanBuildNumber.ps1″>(c) http://TfsBuildExtensions.codeplex.com/. This source is subject to the Microsoft Permissive License. See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx. All other rights reserved.</copyright>

##———————————————————————–

# Update all test plan using the current build definition with the latest build number.

#

  

# Enable -Verbose option

[CmdletBinding()]

  

# Disable parameter

# Convenience option so you can debug this script or disable it in

# your build definition without having to remove it from

# the ‘Post-build script path’ build process parameter.

param([switch]$Disable)

if ($PSBoundParameters.ContainsKey(‘Disable’))

{

   Write-Verbose “Script disabled; no actions will be taken on the files.”

}

  

# If this script is not running on a build server, remind user to

# set environment variables so that this script can be debugged

if(-not $Env:TF_BUILD -and -not ($Env:TF_BUILD_BUILDURI -and $Env:TF_BUILD_BUILDDEFINITIONNAME -and $Env:TF_BUILD_COLLECTIONURI))

{

   Write-Error “You must set the following environment variables”

   Write-Error “to test this script interactively.”

   Write-Error ‘$Env:TF_BUILD_BUILDURI – For example, enter something like:’

   Write-Error ‘$Env:TF_BUILD_BUILDURI = “vstfs:///Build/Build/15″‘

   Write-Error ‘$Env:TF_BUILD_BUILDDEFINITIONNAME – For example, enter something like:’

   Write-Error ‘$Env:TF_BUILD_BUILDDEFINITIONNAME = “MyProduct.Main.CI”‘

   Write-Error ‘$Env:TF_BUILD_COLLECTIONURI – For example, enter something like:’

   Write-Error ‘$Env:TF_BUILD_COLLECTIONURI = “http://localhost:8080/tfs/DefaultCollection”‘

   exit 1

}

  

# Make sure build uri is set

if (-not $Env:TF_BUILD_BUILDURI)

{

   Write-Error (“TF_BUILD_BUILDURI environment variable is missing.”)

   exit 1

}

Write-Verbose “TF_BUILD_BUILDURI: $Env:TF_BUILD_BUILDURI

 

# Make sure build definition name is set

if (-not $Env:TF_BUILD_BUILDURI)

{

   Write-Error (“TF_BUILD_BUILDDEFINITIONNAME environment variable is missing.”)

   exit 1

}

Write-Verbose “TF_BUILD_BUILDDEFINITIONNAME: $Env:TF_BUILD_BUILDDEFINITIONNAME

 

# Make sure tfs collection uri is set

if (-not $Env:TF_BUILD_COLLECTIONURI)

{

   Write-Error (“TF_BUILD_COLLECTIONURI environment variable is missing.”)

   exit 1

}

Write-Verbose “TF_BUILD_BUILDDEFINITIONNAME: $Env:TF_BUILD_BUILDDEFINITIONNAME

 

[Reflection.Assembly]::LoadWithPartialName(‘Microsoft.TeamFoundation.Client’)

[Reflection.Assembly]::LoadWithPartialName(‘Microsoft.TeamFoundation.TestManagement.Client’)

[Reflection.Assembly]::LoadWithPartialName(‘Microsoft.TeamFoundation.Build.Client’)

 

# Find all test plans using this build definition

$tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($env:TF_BUILD_COLLECTIONURI)

$tcm = $tpc.GetService([Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService])

$buildServer = $tpc.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

$teamProject = $buildServer.GetBuild($Env:TF_BUILD_BUILDURI);

$testProject = $tcm.GetTeamProject($teamProject.TeamProject);

$testPlans = $testProject.TestPlans.Query(“SELECT * FROM TestPlan”)

 

$matchingTestPlans = @()

foreach($testPlan in $testPlans)

{

    if($testPlan.BuildFilter.BuildDefinition -eq $Env:TF_BUILD_BUILDDEFINITIONNAME)

    {

        $matchingTestPlans += $testPlan

    }

}

 

# Update test plans with latest build

if($matchingTestPlans)

{

   Write-Host “Will update test plans using $Env:TF_BUILD_BUILDDEFINITIONNAME to $Env:TF_BUILD_BUILDURI to $($matchingTestPlans.count) test plans.”

  

   foreach ($matchingTestPlan in $matchingTestPlans) {

     if(-not $Disable)

     {

        $matchingTestPlan.BuildUri = $Env:TF_BUILD_BUILDURI

        $matchingTestPlan.Save()

        Write-Verbose $matchingTestPlan.Name – version applied”

     }

   }

}

else

{

   Write-Warning “Found no test plans to update.”

}

 

Written by Mathias Olausson

September 16th, 2014 at 10:10 pm

Posted in Uncategorized

3 Responses to 'Automatically update test plan with build number'

Subscribe to comments with RSS or TrackBack to 'Automatically update test plan with build number'.

  1. I’m able to build my project successfully, and deploy it to the target machine, but as soon as the post script is running, and trying to modify and save the test plan, I’m getting an Access Denied message. However I’m able to change the test plan by using MTM (manually – not part of build process). Hence I’m confused why the test plan can’t be saved through the post process. I added the error below. I really appreciate your help.

    Exception calling “Save” with “0” argument(s): “You do not have the appropriate permissions to manage test plans under this area path.”
    At ….\PSscripts\UpdateBuildInUseInTheTestplan.ps1:100 char:4
    + $matchingTestPlan.Save()
    + ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : AccessDeniedException

    2001428

    18 Aug 15 at 7:20 pm

  2. Thanks for the feedback! I looked at this error and it’s happening when the build agent has insufficient permissions. You may solve it by giving the account running the build agent permissions to the test and build artifacts. If that’s not possible I’ve updated the script so that you can pass in a credential and authenticate with that before using the TFS API. See http://blogs.msmvps.com/molausson/2015/10/04/automatically-update-test-plan-with-build-number-take-2/

    Mathias Olausson

    4 Oct 15 at 12:54 pm

  3. Thank you so much for providing this script! Your work is greatly appreciated!

    Ngan Menegay

    13 Feb 17 at 3:18 pm

Leave a Reply