如何以编程方式使用Rest API将测试结果添加到VSTS中的测试运行中

如何以编程方式使用Rest API将测试结果添加到VSTS中的测试运行中

问题描述:

我有此任务的API链接

I've got API link for this task

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

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

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

我需要程序通过API方式完成.

I need program to do through API way.

或者,我尝试使用下面的程序

Alternative, i've tried with program below

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 []"

cannot convert from 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResultUpdateModel[]' to 'Microsoft.TeamFoundation.TestManagement.WebApi.TestCaseResult[]'

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. 安装Microsoft Team Foundation Server扩展客户端程序包[
    安装包Microsoft.TeamFoundationServer.ExtendedClient -版本15.112.1].
  2. 安装Test Manager扩展-创建测试计划,测试套件 测试标签.
  3. Testpointid是测试用例编号[测试用例的顺序/索引在 测试计划],而不是测试用例ID.
  4. 名称是Testcase名称,testrun id是通过以下方式自动捕获的 testpointid [顺序类似1,2,3 ...]
  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...]