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

417 views Asked by At

I have a C# solution that contains automated UI tests. Running those UI tests is a task of a TFS build configured with a C.I. trigger. This automation solution publish test case results using the TFS API with a screenshot attached to each step of the test case.

When I commit again in the repo, since the build has a C.I. trigger, it will run again the tests, and I would like to be able to retrieve the previous screenshot that was generated during the previous build for the test point being executed.

Given my c# solution knows the test case/test plan/test point ID and the branch currently used for the build, what would be the most efficient way to retrieve those captures ?

If anybody has some sample code (I can use the REST or the old API), that would be greatly appreciated :)

2

There are 2 answers

0
LiohAu On BEST ANSWER

I think I may have succedeed to achieve what I want with the following code :

        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;
        }
0
Cece Dong - MSFT On

It seems you are looking for getting a list of test run attachments reference. Check the API below:

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