【问题标题】:Azure DevOps Test API - How to create or get the value of actionPathAzure DevOps 测试 API - 如何创建或获取 actionPath 的值
【发布时间】:2021-03-17 22:58:48
【问题描述】:

澄清, get是指生成或获取我需要提供的数字,而不是测试结果中已经存在的数字。

使用步骤信息(手动运行)创建测试结果时,需要为名为@9​​87654322@的字段提供值

https://docs.microsoft.com/en-us/rest/api/azure/devops/test/action%20results/list?view=azure-devops-rest-6.0#testactionresultmodel

有没有办法找到或生成这个值,以便我可以使用 API 创建测试结果?似乎没有一致或明确的方式来说明什么或在哪里。

【问题讨论】:

    标签: azure-devops-rest-api


    【解决方案1】:

    经过一些尝试/错误后,我找到了解决方案。

    第一阶段 - 获取步骤 ID

    您需要安装HTML Agility Pack nuget 才能从 HTML 中提取数据。

    当创建一个测试步骤时,它会有一个自动的 ID,在它的 HTML 中表示。为了获取测试步骤 HTML 并提取 ID,需要使用以下代码(或类似代码):

    // create a client (assuming you know how to create vss connection)
    var client = vssConnction.GetClient<WorkItemTrackingHttpClient>();
    
    // get the work item with all fields
    var item = client.GetWorkItemAsync(<item id>, expand: WorkItemExpand.All).GetAwaiter().GetResult();
    
    // get the HTML
    var html = $"{item.Fields["Microsoft.VSTS.TCM.Steps"]}"
    
    // load the HTML into DOM object
    var htmlDocument = new HtmlDocument();
    htmlDocument.LoadHtml(html);
    
    // extract all IDs. The actionPath is the hex form of the step ID (.ToString("x"))
    var ids = htmlDocument.DocumentNode.SelectNodes("//step").Select(i => int.Parse(i.GetAttributeValue("id", "0")).ToString("x"));
    

    第二阶段 - 获取行动路径

    actionPath 是 8 位数字的步骤 ID 的十六进制形式,带有前导零(例如,ID 10 将是 0000000a)。为了将 id 解析为操作路径,请使用以下代码(或类似代码):

    var actionPath = ids.Select(i => new string('0', 8 - i.Length) + i);
    

    现在你可以在创建动作测试结果时告诉actionPath

    完整的代码工作流程(未验证错误)

    // credentials
    var basicCredential = new VssBasicCredential("", personalAccessToken);
    var credentials = new VssCredentials(basicCredential);
    
    // connection
    var connection = new VssConnection(new Uri("your collection URI"), credentials);
    
    // clients
    var testManagement = connection.GetClient<TestManagementHttpClient>();
    
    // test points
    var pointsFilter = new PointsFilter { TestcaseIds = new[] { <test_id>, <test_id>, ... } };
    var pointsQuery = new TestPointsQuery() { PointsFilter = pointsFilter };
    var points = testManagement.GetPointsByQueryAsync(query, project).GetAwaiter().GetResult().Points;
    
    // test run
    var runCreateModel = new RunCreateModel(name: "My Test Run", pointIds: points, plan: new ShallowReference(id: $"{<test_plan_id>}"));
    var testRun = testManagement.CreateTestRunAsync(runCreateModel, "<project name>").GetAwaiter().GetResult();
    
    // iteration
    var dateTime = DateTime.Now;
    var date = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond, dateTime.Kind);
    var iteration = new TestIterationDetailsModel
    {
        Id = 1,
        StartedDate = date,
        CompletedDate = date.AddMinutes(5),
        Comment = "My Test Iteration"
    }
    
    // action
    var actionResult = new TestActionResultModel
    {
        ActionPath = "<actionPath>",           // the one we extracted from the HTML
        StepIdentifier = "<the_test_step_id>", // the one we extracted from the HTML
        IterationId = <the_iteration_id>,
        StartedDate = date,
        Outcome = TestOutcome.Passed,
        CompletedDate = date.AddMinutes(5)
    };
    
    iteration.ActionResults = new List<TestActionResultModel> { actionResult };
    
    // test result
    var testCaseResult = testManagement
        .GetTestResultByIdAsync("<project name>", testRun.Id, <test_results_id>, ResultDetails.Iterations)
        .GetAwaiter()
        .GetResult()
        .First();
    testCaseResult.IterationDetails.Add(iteration);
    testManagement.UpdateTestResultsAsync(new[] { testCaseResult }, "<project name>", testRun.Id).GetAwaiter().GetResult();
    

    【讨论】:

    • 感谢您在这里分享您的解决方案,请您接受您的解决方案as the answer?因此,对于遇到相同问题的其他成员轻松找到解决方案将很有帮助。祝你有美好的一天:)
    【解决方案2】:

    如何创建或获取actionPath的值

    您可以使用下面的rest api 来获取actionPath 的值。

    https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results/{testCaseResultId}?detailsToInclude=iterations&api-version=6.0
    

    在 Postman 中测试:

    【讨论】:

    • 这是获取 - 当结果已经存在时,我需要使用 API 创建它们 - 不获取现有结果。这意味着我需要提供 actionPath
    • 我更新了我的问题,以便更清楚地了解 Get 事情
    • 有可能-我每天都这样做,问题是将actionPath与测试ID匹配,请查看我找到的解决方案-步骤ID是十进制,而actionPath是十六进制-十进制,只需转换即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    相关资源
    最近更新 更多