How to find all the automated test runs that happened on a lab environment?

MTM toolset provides ability to execute automated test runs on lab environments and today one of the internal customer asked me how he can programmatically find out all the automated test runs that happened on a lab environment. In this blog, I am sharing how to do that programmatically.

  • Login to a machine with tfs object model installed. (The tfs object model gets installed with VS, MTM, Tfs, test controller etc)
  • Open notepad, copy paste the following script and change the highlighted variables as per your setup/deployment.
  • Open a power-shell command prompt and run the modified power-shell script.
  • It should show all the test runs on the specified environment 

Enjoy !!

# Load Client Assembly
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Common, 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.Lab.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Lab.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
  

# Define parameters
$tfsCollectionUrl = "
https://myserver:8080/tfs/DefaultCollection";
$projectName = "myProject";
$environmentName = "myEnv";

# Connect to tfs
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl);
$tcmService = $tfsCollection.GetService([Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService]);
[Microsoft.TeamFoundation.TestManagement.Client.ITestManagementTeamProject] $tcmProject = $tcmService.GetTeamProject($projectName);
$labService = $tfsCollection.GetService([Microsoft.TeamFoundation.Lab.Client.LabService]);

# Query for environments
$labEnvironmentQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.LabEnvironmentQuerySpec;
$labEnvironmentQuerySpec.Project = $projectName;
$labEnvironmentQuerySpec.Disposition = [Microsoft.TeamFoundation.Lab.Client.LabEnvironmentDisposition]::Active;
$labEnvironments = $labService.QueryLabEnvironments($labEnvironmentQuerySpec);

$matchingEnvironment= $null;
foreach ($labEnvironment in $labEnvironments)
{
$envName = $labEnvironment.Name;
if ($envName -eq $environmentName)
{
$matchingEnvironment = $labEnvironment;
break;
}
}
$environmentId = $matchingEnvironment.LabGuid;

# Query for test runs
$query = [System.String]::Format("select * From TestRun where TestEnvironmentId='{0}' AND IsAutomated=1", $environmentId);
$testRuns = $tcmService.QueryTestRuns($query);

Write-Host ” ================================ “
Write-Host “TestRuns: “$testRuns