检索最后一个测试用例结果,包括与分支中最后一个构建相关的给定测试点

Retrieving last test case result including a given test point that is related to the last build in a branch

我有一个包含自动 UI 测试的 C# 解决方案。 运行 那些 UI 测试是使用 C.I 配置的 TFS 构建任务。扳机。 此自动化解决方案使用 TFS API 发布测试用例结果,并附有测试用例的每个步骤的屏幕截图。

当我在回购中再次提交时,因为构建有一个 C.I。触发,它将再次 运行 测试,我希望能够检索在上一次构建期间为正在执行的测试点生成的上一个屏幕截图。

鉴于我的 C# 解决方案知道测试 case/test plan/test 点 ID 和当前用于构建的分支,检索这些捕获的最有效方法是什么?

如果有人有一些示例代码(我可以使用 REST 或旧的 API),将不胜感激:)

您似乎正在寻找一份测试 运行 附件参考列表。检查下面的 API:

https://docs.microsoft.com/en-us/rest/api/azure/devops/test/attachments/get%20test%20run%20attachments?view=azure-devops-rest-5.1

我想我可能已经成功地用下面的代码实现了我想要的:

        private async Task<GitCommitRef> GetLastCommitOnBranch(TfsProject project, string repositoryId, string branchName)
        {
            GitHttpClient gitHttpClient = GetGitHttpClient(project);
            GitQueryCommitsCriteria gitQueryCommitsCriteria = new GitQueryCommitsCriteria();
            gitQueryCommitsCriteria.Top = 1;
            GitVersionDescriptor gitVersionDescriptor = new GitVersionDescriptor();
            gitVersionDescriptor.Version = branchName.Contains("refs/heads/") ? branchName.Substring("refs/heads/".Length) : branchName;
            gitQueryCommitsCriteria.ItemVersion = gitVersionDescriptor;
            List<GitCommitRef> commits = await gitHttpClient.GetCommitsAsync(repositoryId, gitQueryCommitsCriteria);
            GitCommitRef commitRef = commits.FirstOrDefault();

            return commitRef;
        }

        private async Task<IEnumerable<Build>> GetBuildsForLastCommitOnBranch(TfsProject project, string repositoryId, string branchName)
        {
            GitCommitRef commitRef = await GetLastCommitOnBranch(project, repositoryId, branchName);
            BuildHttpClient buildHttpClient = GetBuildHttpClient(project);
            List<Build> builds = await buildHttpClient.GetBuildsAsync(project.TfsProjectName, branchName: branchName, minFinishTime: commitRef.Author.Date, repositoryType: RepositoryTypes.Git);
            IEnumerable<Build> buildsContainingLastCommit = builds.Where(b => b.SourceVersion == commitRef.CommitId);

            return buildsContainingLastCommit;
        }

        public async Task<byte[]> GetActionScreenshotForLastCommitInBranch(TfsProject project, string repositoryId, string targetBranch, string currentBuildUrl, int testPlanId, int testCaseId, int testPointId, int iterationId, int actionId)
        {
            // Try to retrieve the builds containing the latest commit on target branch
            IEnumerable<Build> buildsContainingLastCommit = await GetBuildsForLastCommitOnBranch(project, repositoryId, targetBranch);

            ITestManagementTeamProject testManagementTeamProject = GetTeamProject(project);
            ITestCase testCase = testManagementTeamProject.TestCases.Find(testCaseId);
            ITestCaseResultCollection testCaseResultCollection = testManagementTeamProject.TestResults.ByTestId(testCaseId);

            // Try to find a result for the test point that has been generated by a build related to the latest commit of the target branch
            ITestCaseResult testCaseResult = testCaseResultCollection.FirstOrDefault(tcr => tcr.TestPointId == testPointId && tcr.TestCaseRevision == testCase.Revision && buildsContainingLastCommit.Count(b => b.BuildNumber == tcr.BuildNumber) > 0);
            if (testCaseResult != null)
                return GetActionScreenshotFromTestCaseResult(testCaseResult, iterationId, actionId);

            return null;
        }