使用Microsoft.TeamFoundation.TestManagement.WebApi发布Azure测试结果时无法添加生成编号
解决Azure测试运行中单个测试结果添加生成编号的方案
核心思路
Azure DevOps的TestRun级别的BuildNumber是全局属性,无法为单个测试用例结果单独设置字符串形式的生成编号。要实现单个测试绑定对应生成编号,需通过TestCaseResult的Build属性关联目标生成的完整资源对象,而非直接赋值编号字符串。
具体实现步骤
获取目标生成的资源对象
先通过Build API获取对应生成编号的Build实例,核心是拿到它的Id和Uri:var buildClient = new BuildHttpClient(new Uri(collectionUrl), new VssBasicCredential("", personalAccessToken)); var builds = await buildClient.GetBuildsAsync(projectName, buildNumber: "你的目标生成编号"); var targetBuild = builds.FirstOrDefault(); if (targetBuild == null) throw new Exception("未找到对应生成编号的构建资源");为测试结果绑定生成资源
创建或更新TestCaseResult时,直接将Build属性赋值为获取到的targetBuild,系统会自动关联并显示对应的生成编号:var testCaseResult = new TestCaseResult { TestCaseTitle = "你的测试用例名称", Outcome = TestOutcome.Passed, State = TestResultState.Completed, Build = targetBuild // 绑定目标生成,自动带入生成编号信息 };提交测试结果
使用TestManagementHttpClient提交结果:var testManagementClient = new TestManagementHttpClient(new Uri(collectionUrl), new VssBasicCredential("", personalAccessToken)); await testManagementClient.CreateTestResultsAsync(new[] { testCaseResult }, projectName, testRunId);
关键注意事项
- 不要尝试通过
TestRun或RunUpdateModel设置单个测试的生成编号,这些对象的BuildNumber是测试运行的全局属性,无法覆盖单个测试结果的关联生成。 - 确保你的Personal Access Token(PAT)拥有**Build(读取)和Test Management(写入)**权限,否则无法获取生成信息或提交测试结果。
内容的提问来源于stack exchange,提问作者fire hawks
相关产品推荐
相关产品推荐

