将结果发布到TFS时找不到Build

本文关键字:找不到 Build TFS 结果 | 更新日期: 2023-09-27 18:10:46

我试图通过TFS将测试结果发布到Microsoft测试管理器,但当试图通过TFS API发布结果时,我得到以下错误:

Microsoft.TeamFoundation.TestManagement.Client.TestObjectNotFoundException:
Build vstfs:///Build/Build/### cannot be found.

我得到了我的测试计划,并按照微软建议的方式创建了一个测试运行:

plan = teamProject.TestPlans.Query(
    "SELECT * FROM TestPlan WHERE PlanName = '" + planName + "')[0];
ITestRun run = plan.CreateTestRun(true);
run.AddTestPoints(testPoints, null);
run.Save();

那么为什么它说无法找到构建?

将结果发布到TFS时找不到Build

构建会定期从系统中清除,因此您可能希望确保使用的是最新的构建。然后,您可以获得最新版本的URI,并将其与您的运行相关联。

Uri GetLatestBuildURI(string projectName) {
    Uri buildUri = null;
    IBuildServer buildServer = _tfs.GetService<IBuildServer>();
    IBuildDetailSpec detailSpec = buildServer.CreateBuildDetailSpec(projectName);
    detailSpec.MaxBuildsPerDefinition = 1;
    detailSpec.QueryOrder = BuildQueryOrder.FinishTimeDescending;
    IBuildQueryResult results = buildServer.QueryBuilds(detailSpec);
    if (results.Builds.Length == 1) {
        IBuildDetail detail = results.Builds[0];
        buildUri = detail.Uri;
    }
    return buildUri;
}

ITestRun run = plan.CreateTestRun(true);
run.BuildUri = GetLatestBuildURI(projectName);
run.AddTestPoints(testPoints, null);
run.Save();