简体   繁体   English

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

[英]How to retrieve all Test Cases in a Test Plan via TFS API?

Using the TFS 2015 API, I am attempting to get all test cases under a test plan. 我正在尝试使用TFS 2015 API将所有测试用例都放在一个测试计划下。 I am currently running into the issue where I am retrieving all the test cases in a test plan, but the way I am pulling in the test cases is giving me collection of distinct test cases in a test plan. 我目前遇到的问题是,我要在测试计划中检索所有测试用例,但是我提取测试用例的方式是让我在测试计划中收集不同的测试用例。 I would like all test case IDs, including duplicate test case IDs in the event we are testing various configurations of a particular test in that test plan. 我想要所有测试用例ID,包括重复的测试用例ID,以备我们在该测试计划中测试特定测试的各种配置时使用。

I ultimately want to be able to access the result of all configurations of a test case via the API. 我最终希望能够通过API访问测试用例的所有配置结果。

Below is the method I am using to pull in test cases for a specified test plan ID: 以下是我用于获取指定测试计划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;
}

Say I have a test plan with 10 distinct test cases with two different configurations for each case. 假设我有一个包含10个不同测试用例的测试计划,每个用例有两种不同的配置。 Therefore there will ultimately be 20 test case scenarios in that plan now with 20 possible outcomes during the test run. 因此,该计划现在最终将有20个测试用例场景,在测试运行期间将有20种可能的结果。 Using the above method, it is only pulling in the 10 distinct test cases. 使用以上方法,它仅拉入10个不同的测试用例。

I have tried drilling down into each ITestSuiteEntry in the plan, using the below method, but it only hits the suites directly under the plan. 我尝试使用以下方法ITestSuiteEntry计划中的每个ITestSuiteEntry ,但它仅会直接影响计划中的套件。 It doesn't account for nested suites. 它不考虑嵌套套件。

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);
        }
    }
}

How do I access every configuration of every test case in a plan so I can collect their test outcomes? 如何访问计划中每个测试用例的每个配置,以便收集它们的测试结果? Is there something obvious I am missing here? 有什么明显的我想念的地方吗?

You can have a try with below powershell script: 您可以尝试使用以下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 "-----------"
}

Reference: 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM