【问题标题】:How to create a test run and result using the Team Foundation Server API?如何使用 Team Foundation Server API 创建测试运行和结果?
【发布时间】:2011-09-24 06:00:49
【问题描述】:

我找到了几个关于使用 TFS API 检索测试结果的示例,但没有关于以编程方式创建结果的文档。我的目标是创建一个轻量级的替代方案,以使用 Microsoft 测试管理器来运行手动测试。有任何人对此有经验吗?有没有我遗漏的例子?

这是我目前所拥有的:

ITestCaseResult CreateNewTestCaseResult(ITestSuiteEntry testCaseEntry)
{
    var run = testCaseEntry.TestSuite.Plan.CreateTestRun(false /* not automated */);
    run.AddTest(testCaseEntry.TestCase.Id, suiteEntry.TestSuite.DefaultConfigurations[0].Id, suiteEntry.TestSuite.Plan.Owner);
    run.Save(); // so that results object is created
    return run.QueryResults()[0];
}

我不确定这是否是启动新运行的正确方法,也不确定如何记录测试的每个操作的结果。

【问题讨论】:

    标签: c# visual-studio-2010 tfs-sdk microsoft-test-manager


    【解决方案1】:

    2012 年 8 月 15 日更新:

    下面的示例现已集成到我的开源 TFS 测试步骤编辑器工具中。在最新版本中,它获得了将测试结果发布到 TFS 的能力。请参阅GitHub 上的源代码。


    我现在有用于发布测试结果的工作代码。请注意,以下代码接受 ITestPoint(这表示特定套件中的测试用例)并有一些我的内部类(不包括在内),它们仅提供每个步骤的结果和附件路径。

    var tfsRun = _testPoint.Plan.CreateTestRun(false);
    
    tfsRun.DateStarted = DateTime.Now;
    tfsRun.AddTestPoint(_testPoint, _currentIdentity);
    tfsRun.DateCompleted = DateTime.Now;
    tfsRun.Save(); // so results object is created
    
    var result = tfsRun.QueryResults()[0];
    result.Owner = _currentIdentity;
    result.RunBy = _currentIdentity;
    result.State = TestResultState.Completed;
    result.DateStarted = DateTime.Now;
    result.Duration = new TimeSpan(0L);
    result.DateCompleted = DateTime.Now.AddMinutes(0.0);
    
    var iteration = result.CreateIteration(1);
    iteration.DateStarted = DateTime.Now;
    iteration.DateCompleted = DateTime.Now;
    iteration.Duration = new TimeSpan(0L);
    iteration.Comment = "Run from TFS Test Steps Editor by " + _currentIdentity.DisplayName;
    
    for (int actionIndex = 0; actionIndex < _testEditInfo.TestCase.Actions.Count; actionIndex++)
    {
        var testAction = _testEditInfo.TestCase.Actions[actionIndex];
        if (testAction is ISharedStepReference)
            continue;
    
        var userStep = _testEditInfo.SimpleSteps[actionIndex];
    
        var stepResult = iteration.CreateStepResult(testAction.Id);
        stepResult.ErrorMessage = String.Empty;
        stepResult.Outcome = userStep.Outcome;
    
        foreach (var attachmentPath in userStep.AttachmentPaths)
        {
            var attachment = stepResult.CreateAttachment(attachmentPath);
            stepResult.Attachments.Add(attachment);
        }
    
        iteration.Actions.Add(stepResult);
    }
    
    var overallOutcome = _testEditInfo.SimpleSteps.Any(s => s.Outcome != TestOutcome.Passed)
        ? TestOutcome.Failed
        : TestOutcome.Passed;
    
    iteration.Outcome = overallOutcome;
    
    result.Iterations.Add(iteration);
    
    result.Outcome = overallOutcome;
    result.Save(false);
    

    【讨论】:

      【解决方案2】:

      测试操作似乎没有设置通过/失败或添加附件的属性。

      public interface ITestAction : INotifyPropertyChanged {
          int Id { get; }
          ITestBase Owner { get; }
          ITestActionGroup Parent { get; }
      
          ITestAction CopyToNewOwner(ITestBase newOwner);
          void MoveToNewOwner(ITestBase newOwner); }
      

      这是在父级(TestCase)完成的。

      ITestCaseResult result = run.QueryResults()[0];
      IAttachmentCollection collection = result.Attachments;
      string x = result.Comment;
      

      以下是正确启动新运行的方法:

      namespace SampleRunCreation
      {
          class Program
          {
              static void Main(string[] args)
              {
                  TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://pradeepn-tcm:8080/tfs/DefaultCollection"));
                  ITestManagementTeamProject project = tfs.GetService<ITestManagementService>().GetTeamProject("Pradeep");
      
                  // Create a test case.
                  ITestCase testCase = CreateTestCase(project, "My test case");
      
                  // Create test plan.
                  ITestPlan plan = CreateTestPlan(project, "My test plan");
      
                  // Create test configuration. You can reuse this instead of creating a new config everytime.
                  ITestConfiguration config = CreateTestConfiguration(project, string.Format("My test config {0}", DateTime.Now));
      
                  // Create test points. 
                  IList<ITestPoint> testPoints = CreateTestPoints(project,
                                                                  plan,
                                                                  new List<ITestCase>(){testCase}, 
                                                                  new IdAndName[] { new IdAndName(config.Id, config.Name) });
      
                  // Create test run using test points.
                  ITestRun run = CreateTestRun(project, plan, testPoints);
      
                  // Query results from the run.
                  ITestCaseResult result = run.QueryResults()[0];
      
                  // Fail the result.
                  result.Outcome = TestOutcome.Failed;
                  result.State = TestResultState.Completed;
                  result.Save();
      
                  Console.WriteLine("Run {0} completed", run.Id);
              }
      
              private static ITestCase CreateTestCase(ITestManagementTeamProject project,
                                                      string title)
              {
                  // Create a test case.
                  ITestCase testCase = project.TestCases.Create();
                  testCase.Owner = null;
                  testCase.Title = title;
                  testCase.Save();
                  return testCase;
              }
      
              private static ITestPlan CreateTestPlan(ITestManagementTeamProject project, string title)
              {
                  // Create a test plan.
                  ITestPlan testPlan = project.TestPlans.Create();
                  testPlan.Name = title;
                  testPlan.Save();
                  return testPlan;
              }
      
              private static ITestConfiguration CreateTestConfiguration(ITestManagementTeamProject project, string title)
              {
                  ITestConfiguration configuration = project.TestConfigurations.Create();
                  configuration.Name = title;
                  configuration.Description = "DefaultConfig";
                  configuration.Values.Add(new KeyValuePair<string, string>("Browser", "IE"));
                  configuration.Save();
                  return configuration;
              }
      
              public static IList<ITestPoint> CreateTestPoints(ITestManagementTeamProject project,
                                                               ITestPlan testPlan, 
                                                               IList<ITestCase> testCases, 
                                                               IList<IdAndName> testConfigs)
              {
                  // Create a static suite within the plan and add all the test cases.
                  IStaticTestSuite testSuite = CreateTestSuite(project);
                  testPlan.RootSuite.Entries.Add(testSuite);
                  testPlan.Save();
      
                  testSuite.Entries.AddCases(testCases);
                  testPlan.Save();
      
                  testSuite.SetEntryConfigurations(testSuite.Entries, testConfigs);
                  testPlan.Save();
      
                  ITestPointCollection tpc = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE SuiteId = " + testSuite.Id);
                  return new List<ITestPoint>(tpc);
              }
      
              private static IStaticTestSuite CreateTestSuite(ITestManagementTeamProject project)
              {
                  // Create a static test suite.
                  IStaticTestSuite testSuite = project.TestSuites.CreateStatic();
                  testSuite.Title = "Static Suite";
                  return testSuite;
              }
      
              private static ITestRun CreateTestRun(ITestManagementTeamProject project,
                                                   ITestPlan plan,
                                                   IList<ITestPoint> points)
              {
                  ITestRun run = plan.CreateTestRun(false);
                  foreach (ITestPoint tp in points)
                  {
                      run.AddTestPoint(tp, null);
                  }
      
                  run.Save();
                  return run;
              }
          }
      }
      

      Reference

      【讨论】:

      • 这是一个开始,基本上与我在示例中的观点相同。我正在寻找的是记录测试用例中每个操作的通过/失败、设置 cmets 并将文件附加到每个操作。
      • 我不认为通过阅读 API 可以完全按照自己的意愿去做。 API 内部的 cmets/attachments 和操作之间没有关联。
      • 我又读了一些书,我想我们快到了。 ITestCaseResult 有Iterations(ITestIterationResult 的集合),每个都有Actions(ITestActionResult 的集合),每个都有Outcome 和Comment。我会留下一些东西并报告回来。
      • 但是如何为每个操作添加附件?这很酷,是的,让我知道学习新东西总是很好。
      • 哦,我的错,ITestActionResult 也有附件集合!将很快发布代码。感谢您为我指明正确的方向。
      猜你喜欢
      • 2016-03-09
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多