How to Add test results to a test run in VSTS using Rest API programatically

后端 未结 2 1238
暗喜
暗喜 2021-01-17 06:48

I\'ve got API link for this task

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

相关标签:
2条回答
  • 2021-01-17 07:11

    Refer to these steps:

    1. Install Microsoft Team Foundation Server Extended Client package
    2. Simple code

    :

     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 讨论(0)
  • 2021-01-17 07:17
    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);
    }
    

    Note: Points to Configure, if needed

    1. Install Microsoft Team Foundation Server Extended Client package [
      Install-Package Microsoft.TeamFoundationServer.ExtendedClient -Version 15.112.1].
    2. Install Test Manager extension - Create test plan, test suite in Test tab.
    3. Testpointid is Testcase no [order/index of testcase in test plan], not testcase id.
    4. Name is Testcase name, testrun id is auto captures through testpointid[order like 1,2,3...]
    0 讨论(0)
提交回复
热议问题