2017-06-12 51 views
0

我有API鏈接,這個任務VSTS如何測試結果添加到測試運行使用REST API編程

POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results?api-version={version}

https://www.visualstudio.com/en-us/docs/integrate/api/test/results#add-test-results-to-a-test-run

我需要的程序做通過API方式。

另類,我試過用程序如下

public void GetResult() { var u = new Uri("https://{UserAccount}.visualstudio.com"); VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "MyPAT")); var connection = new VssConnection(u, c); var testClient = connection.GetClient<TestManagementHttpClient>(); int testpointid = 100; string teamProject = "Project12345"; RunCreateModel run = new RunCreateModel(name: "APIRun7", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100"), pointIds: new int[] { testpointid }); TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result; TestCaseResultUpdateModel testCaseUpdate = new TestCaseResultUpdateModel() { State = "Completed", Outcome = "Passed", TestResult = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("100000") }; //var testResults = testClient.UpdateTestResultsAsync(new TestCaseResultUpdateModel[] { testCaseUpdate }, teamProject, testrun.Id).Result; RunUpdateModel runmodel = new RunUpdateModel(state: "Completed"); TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel ,teamProject, testrun.Id, runmodel).Result; }​

我得到了錯誤: 無法從 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResultUpdateModel []' 到「Microsoft.TeamFoundation轉換.TestManagement.WebApi.TestCaseResult []」

+0

我的計劃力鍛鍊......所以只能在這裏發帖求助 – Shalem

+0

你是使用15.0包,所以改變代碼是這樣的:TestCaseResult caseResult = new TestCaseResult(){State =「Completed」,Outcome =「passed」,Id = 100000}; var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult [] {caseResult},teamProject,testrun.Id).Result; –

+0

Remove TestCaseResultUpdateModel testCaseUpdate = new TestCaseResultUpdateModel(){State =「Completed」,Outcome =「Passed」,TestResult = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(「100000」)}; –

回答

1
 try 
     { 
      var u = new Uri("https://{My Account}.visualstudio.com"); 
      VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "PAT")); 
      var connection = new VssConnection(u, c); 
      var testClient = connection.GetClient<TestManagementHttpClient>(); 
      int testpointid = 1; 
      string teamProject = "MyProjectName"; 
      RunCreateModel run = new RunCreateModel(name: "TestCase Name", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("TestPlan Id"), pointIds: new int[] { testpointid }); 
      TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result; 

      TestCaseResult caseResult = new TestCaseResult() { State = "Completed", Outcome = "passed", Id = 100000 }; 

      var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult[] { caseResult }, teamProject, testrun.Id).Result; 
      RunUpdateModel runmodel = new RunUpdateModel(state: "Completed"); 
      TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel, teamProject, testrun.Id, runmodel).Result; 

     } 
     catch (AggregateException e) 
     { 
      Console.WriteLine(e.InnerException.Message); 
     } 

注:點配置,如果需要的話

  1. 安裝微軟的Team Foundation Server擴展客戶端軟件包[
    安裝程序包Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1]。
  2. 安裝測試管理器擴展 - 在 測試選項卡中創建測試計劃,測試套件。
  3. Testpointid是Testcase否[ 測試計劃中的測試用例的順序/索引],而不是testcase標識。
  4. 名稱是測試用例的名字,testrun ID是自動捕獲通過 testpointid [爲了像1,2,3 ...]
+0

@starain - 你的幫助非常有幫助,真誠讚賞。謝謝 – Shalem

1

參見下列步驟:

  1. 安裝Microsoft Team Foundation Server Extended Client
  2. 簡單的代碼

var u = new Uri("https://[account].visualstudio.com"); 
       VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "[personal access token]"));     
var connection = new VssConnection(u, c); 
       var testClient = connection.GetClient<TestManagementHttpClient>(); 

       string teamProject = "scrum2015"; 
       int testRunId = 286; 
       int testSuiteId = 591; 
       RunUpdateModel runmodelUpdate = new RunUpdateModel(state: "InProgress"); 

       TestRun testRunUpdateResult = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate).Result; 
       var results = testClient.GetTestResultsAsync(teamProject, testRunId).Result.First(); 
       var testPoints= testClient.GetPointsAsync(teamProject,Int32.Parse(testRunUpdateResult.Plan.Id), testSuiteId).Result; 
       TestRun testRunUpdate = testClient.GetTestRunByIdAsync(teamProject, testRunId).Result; 

       TestResultCreateModel newTestResult = new TestResultCreateModel() { TestCase=new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: results.TestCase.Id.ToString()), TestPoint = new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference(id: testPoints.First().Id.ToString()), Outcome="Failed",State= "Completed" }; 
       var updateResult= testClient.AddTestResultsToTestRunAsync(new TestResultCreateModel[] { newTestResult }, teamProject, testRunId).Result; 
       RunUpdateModel runmodelUpdate2 = new RunUpdateModel(state: "Completed"); 

       TestRun testRunUpdateResult2 = testClient.UpdateTestRunAsync(teamProject, testRunId, runmodelUpdate2).Result; 
+0

@starian - 對我來說是好開始。 TestSuite(testSuiteId)不存在於我的VSTS中。直接測試。你能否幫助修改/更新相應的代碼...... 這段代碼對其他人會非常有用。不要忽略這個(可能會附加另一個) – Shalem

+0

我更新了我的代碼和錯誤。請仔細查看。如果有任何更新/錯誤解決方案,請諮詢您的幫助 – Shalem

+1

@Shalem不,測試點必須位於TestSuite中,您需要創建一個TestSuite並添加/創建測試用例。 –

相關問題