Get all Test Cases from Test Plan

09/05/2013 18:11

Get all Test Cases from Test Plan in Powershell

script was created from C# code and inspired by https://ddasankaindrajith.blogspot.sk/2013/04/how-to-create-test-plans-and-cases.html

One additional 'WindowsBase' reference was needed.


  $ProjectName = "YourProjectName"
 $TfsServerUrl = "https://tfs.yourTfsUrl"
 $testPlanName = "Your Test Plan Name"
 

cls

#===================================================
#     Settings
#===================================================


Write-Host "Input parameters: "

$ParamObject = @{
 'ProjectName' = $ProjectName;
 'TfsServerUrl' = $TfsServerUrl;
 'testPlanName' = $testPlanName;
}

$ParamObject | Format-Table

 
#===================================================
#    Main Script Body
#===================================================


  #load assemblies
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client")
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client")
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.TestManagement.Common")
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.TestManagement.Client")

    #get TFS structure, project

 $tfs = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection([Microsoft.TeamFoundation.Client.TfsTeamProjectCollection]::GetFullyQualifiedUriForName($tfsServerUrl));
 $tms = $tfs.GetService([Microsoft.TeamFoundation.TestManagement.Client.ITestManagementService]);
 $project = [Microsoft.TeamFoundation.TestManagement.Client.ITestManagementTeamProject]$tms.GetTeamProject($ProjectName);

 #powershell bug workaround, $project.TestPlans property is not available
 $testPlansProperty = [Microsoft.TeamFoundation.TestManagement.Client.ITestManagementTeamProject].GetProperty("TestPlans").GetGetMethod();
 $testPlans = $testPlansProperty.Invoke($project, "instance,public", $null, $null, $null);

#   List all Test Plans
 foreach($p in $testPlans.Query("Select * From TestPlan"))
 {
  "Plan - {0} : {1}" -f $p.Id, $p.Name
 }

 $currentPlan = $testPlans.Query("Select * From TestPlan Where planName = '{0}'" -f $testPlanName)
 $plan = $currentPlan | where {$_.Name -eq $testPlanName}
 
 $testCases = $plan.RootSuite.AllTestCases
 
 $i=1
 foreach($testCase in $testCases)
 {
  write-host ""
  write-host "-----------"
  write-host "Test Case " $i
  write-host "-----------"
  $testCase.Title
  $testCase.Id
  $testCase.CustomFields["Description HTML"].Value
  
  $i++
   
 }