13

Tôi đã tìm thấy một số mẫu về truy xuất kết quả thử nghiệm bằng cách sử dụng API TFS, nhưng không có tài liệu nào về cách tạo kết quả theo chương trình. Mục tiêu của tôi là tạo ra một thay thế nhẹ để sử dụng Microsoft Test Manager để chạy thử nghiệm thủ công. Có ai có kinh nghiệm với điều này? Có bất kỳ ví dụ nào ở đó mà tôi đang thiếu không?Cách tạo chạy thử và kết quả bằng cách sử dụng API của Team Foundation Server?

Dưới đây là những gì tôi có cho đến nay:

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

Tôi không chắc chắn nếu điều này là cách chính xác để initate một chạy mới, và tôi không chắc chắn làm thế nào để ghi lại kết quả cho mỗi hành động của kiểm tra.

Trả lời

12

Cập nhật ngày 15 tháng 8 năm 2012:

Các mẫu dưới đây hiện đã được tích hợp vào mở của tôi nguồn TFS Test Steps Editor công cụ. Trong phiên bản mới nhất, nó đã đạt được khả năng xuất bản kết quả kiểm tra lên TFS. Xem nguồn trên GitHub.


Tôi hiện đang có mã làm việc để xuất bản kết quả kiểm tra. Lưu ý, mã sau chấp nhận ITestPoint (đây là trường hợp thử nghiệm trong một bộ cụ thể) và có một số lớp bên trong của tôi (không bao gồm) chỉ cung cấp đường dẫn kết quả và tệp đính kèm cho mỗi bước.

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

Kiểm tra Hành động dường như không có thuộc tính cho việc đặt pass/fail hoặc để thêm tệp đính kèm.

public interface ITestAction : INotifyPropertyChanged { 
    int Id { get; } 
    ITestBase Owner { get; } 
    ITestActionGroup Parent { get; } 

    ITestAction CopyToNewOwner(ITestBase newOwner); 
    void MoveToNewOwner(ITestBase newOwner); } 

Điều đó được thực hiện ở cấp độ gốc (TestCase).

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

Sau đây là cách bạn bắt đầu một cách chính xác một hoạt động mới:

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

+0

Đó là sự khởi đầu, về cơ bản cùng một điểm tôi đã nhận được trong ví dụ của mình. Những gì tôi đang tìm kiếm là ghi vượt qua/thất bại cho mỗi hành động trong trường hợp kiểm tra, thiết lập ý kiến, và đính kèm tập tin cho mỗi hành động. –

+0

Tôi sẽ cập nhật câu trả lời của mình. –

+0

Tôi không nghĩ đến việc đọc qua API mà bạn có thể làm chính xác những gì bạn muốn làm. Không có liên kết nào giữa các nhận xét/tệp đính kèm và hành động bên trong API. –

Các vấn đề liên quan