How to find the test runs associated with a build?
Visual studio build summary page shows a lot of information about the build and one of them is about test runs associated with that build. In my work, I had to find out the test run Ids associated with a given build but since the Ids are not exposed in the summary page or logs, it is slightly hard to find if the test runs are not associated with a test plan which was true for my case.
There are a # of articles available on how to find it using one or the other tool (see Tarun’s blog for one example) but I needed something more light-weight. Since these days, I am into power-shell, I quickly coded something and thought of sharing with you all.
Enjoy !!
# Load Client Assembly
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.TestManagement.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.TestManagement.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Build.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Build.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
# Define parameters
$tfsCollectionUrl = “ https://myserver:8080/tfs/MyCollection”;
$projectName = "MyProject";
$buildDefinitionName = "MyDefinition";
# Find the last build for this definition
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl);
$buildService = $tfsCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]);
[Microsoft.TeamFoundation.Build.Client.IBuildDefinition] $buildDefinition = $buildService.GetBuildDefinition($projectName, $buildDefinitionName);
$build = $buildDefinition.LastBuildUri;
# get the test runs associated with the build
$tcmService = $tfsCollection.GetService([Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService]);
[Microsoft.TeamFoundation.TestManagement.Client.ITestManagementTeamProject] $tcmProject = $tcmService.GetTeamProject($projectName);
$testRunHelper = $tcmProject.TestRuns;
$testRuns = $testRunHelper.ByBuild($build);
foreach ($testRun in $testRuns)
{
Write-Host "RunId -> " $testRun.Id
}