【发布时间】:2021-03-17 22:58:48
【问题描述】:
澄清, get是指生成或获取我需要提供的数字,而不是测试结果中已经存在的数字。
使用步骤信息(手动运行)创建测试结果时,需要为名为@987654322@的字段提供值
有没有办法找到或生成这个值,以便我可以使用 API 创建测试结果?似乎没有一致或明确的方式来说明什么或在哪里。
【问题讨论】:
澄清, get是指生成或获取我需要提供的数字,而不是测试结果中已经存在的数字。
使用步骤信息(手动运行)创建测试结果时,需要为名为@987654322@的字段提供值
有没有办法找到或生成这个值,以便我可以使用 API 创建测试结果?似乎没有一致或明确的方式来说明什么或在哪里。
【问题讨论】:
经过一些尝试/错误后,我找到了解决方案。
您需要安装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();
【讨论】:
如何创建或获取actionPath的值
您可以使用下面的rest api 来获取actionPath 的值。
https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results/{testCaseResultId}?detailsToInclude=iterations&api-version=6.0
在 Postman 中测试:
【讨论】: