如何通过TFS API检索测试计划中的所有测试用例?

时间:2017-12-15 16:27:35

标签: c# tfs

使用TFS 2015 API,我试图将所有测试用例纳入测试计划。我目前遇到的问题是我在测试计划中检索所有测试用例,但是我在测试用例中的方式是在测试计划中给出了 distinct 测试用例的集合。我希望所有测试用例ID,包括在我们测试该测试计划中特定测试的各种配置的情况下重复的测试用例ID。

我最终希望能够通过API访问测试用例的所有配置的结果。

以下是我用于提取指定测试计划ID的测试用例的方法:

public ITestCaseCollection
TestPlan_GetAllTestCasesByPlanId(ITestManagementTeamProject testproject, int id)
{
    var plans = testproject.TestPlans.Query("Select * From TestPlan");
    var planById = plans.First(x => x.Id == id);
    return planById.RootSuite.AllTestCases;
}

假设我有一个包含10个不同测试用例的测试计划,每个案例有两种不同的配置。因此,该计划最终将有20个测试用例场景,在测试运行期间有20个可能的结果。使用上述方法,它只涉及10个不同的测试用例。

我尝试使用下面的方法深入到计划中的每个ITestSuiteEntry,但它只会直接命中计划下的套件。它没有考虑嵌套套件。

public ITestCaseResultCollection GetTestCaseResults(ITestManagementTeamProject testproject, int id)
{
    var plans = testproject.TestPlans.Query("Select * from TestPlan");
    var planById = plans.First(x => x.Id == id);

    foreach(ITestSuiteEntry suiteEntry in planById.RootSuite.Entries)
    {
        var testSuite = suiteEntry.TestSuite;
        foreach(ITestSuiteEntry subSuiteEntry in testSuite.TestCases)
        {    
            var testCaseResults = testproject.TestResults.ByTestId(subSuiteEntry.TestCase.Id);
        }
    }
}

如何访问计划中每个测试用例的每个配置,以便我可以收集他们的测试结果?我有什么明显的遗失吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下powershell脚本:

$ProjectName = "foo"
$TfsServerUrl = "http://tfsserverurl:8080/tfs/bar"
$testPlanName = "testplanname"

#===================================================
#     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


 foreach($testCase in $testCases)
 {
  write-host ""
  write-host "-----------"
  write-host "START - Test Case"  $testCase.Id
   write-host "Parameters:"

$testCase.TestSuiteEntry.TestCase.DefaultTable  
  write-host "-----------"
}

参考: