Azure DevOps 测试 API - 如何创建或获取 actionPath 的值

Azure DevOps Test API - How to create or get the value of actionPath

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

使用步骤信息创建测试结果时(手动 运行),需要为名为 actionPath

的字段提供值

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

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

How to create or get the value of actionPath

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

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

在 Postman 中测试:

经过一些 try/error 我找到了解决方案。

第一阶段 - 获取步骤 ID

You need to install HTML Agility Pack nuget to be able to extract data from the 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();