Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索上一个测试用例结果,包括与分支中最后一个生成相关的给定测试点_C#_Tfs_Azure Devops Rest Api - Fatal编程技术网

C# 检索上一个测试用例结果,包括与分支中最后一个生成相关的给定测试点

C# 检索上一个测试用例结果,包括与分支中最后一个生成相关的给定测试点,c#,tfs,azure-devops-rest-api,C#,Tfs,Azure Devops Rest Api,我有一个包含自动化UI测试的C#解决方案。运行这些UI测试是配置了C.I.触发器的TFS构建的任务。 此自动化解决方案使用TFSAPI发布测试用例结果,并在测试用例的每个步骤上附加一个屏幕截图 当我在repo中再次提交时,由于构建有一个C.I.触发器,它将再次运行测试,并且我希望能够检索上一次构建期间为正在执行的测试点生成的上一个屏幕截图 如果我的c#解决方案知道测试用例/测试计划/测试点ID和当前用于构建的分支,那么检索这些捕获的最有效方法是什么 如果有人有一些示例代码(我可以使用REST或旧

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

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

如果我的c#解决方案知道测试用例/测试计划/测试点ID和当前用于构建的分支,那么检索这些捕获的最有效方法是什么


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

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


我想我可能已经成功地通过以下代码实现了我想要的:

        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;
        }
私有异步任务GetLastCommitOnBranch(TfsProject项目,字符串repositoryId,字符串branchName)
{
GitHttpClient GitHttpClient=GetGitHttpClient(项目);
GitQueryCommitsCriteria GitQueryCommitsCriteria=新的GitQueryCommitsCriteria();
gitQueryCommitsCriteria.Top=1;
GitVersionDescriptor GitVersionDescriptor=新GitVersionDescriptor();
gitVersionDescriptor.Version=branchName.Contains(“refs/heads/”)?branchName.Substring(“refs/heads/”.Length):branchName;
gitQueryCommitsCriteria.ItemVersion=gitVersionDescriptor;
列表提交=等待gitHttpClient.GetCommitsAsync(repositoryId,gitQueryCommitsCriteria);
GitCommitRef commitRef=commits.FirstOrDefault();
返还佣金;
}
私有异步任务GetBuildsForLastCommittenBranch(TfsProject项目,字符串repositoryId,字符串branchName)
{
GitCommitRef commitRef=await GetLastCommitOnBranch(项目、存储ID、branchName);
BuildHttpClient BuildHttpClient=GetBuildHttpClient(项目);
List builds=Wait buildHttpClient.GetBuildsAsync(project.TfsProjectName,branchName:branchName,minFinishTime:commitRef.Author.Date,repositoryType:RepositoryTypes.Git);
IEnumerable buildsContainingLastCommit=builds.Where(b=>b.SourceVersion==commitRef.CommitId);
返回buildsContainingLastCommit;
}
公共异步任务GetActionScreenshotForLastCommitInBranch(TfsProject项目、字符串repositoryId、字符串targetBranch、字符串currentBuildUrl、int-testPlanId、int-testCaseId、int-testPointId、int-iterationId、int-actionId)
{
//尝试检索包含目标分支上最新提交的生成
IEnumerable buildsContainingLastCommit=等待GetBuildsForLastCommitOnBranch(项目、存储ID、目标分支);
ITestManagementTeamProject测试管理teamproject=GetTeamProject(项目);
ITestCase testCase=testManagementTeamProject.TestCases.Find(testCaseId);
ITestCaseResultCollection testCaseResultCollection=testManagementTeamProject.TestResults.ByTestId(testCaseId);
//尝试查找由与目标分支的最新提交相关的生成生成的测试点的结果
ITestCaseResult testCaseResult=testCaseResultCollection.FirstOrDefault(tcr=>tcr.TestPointId==TestPointId&&tcr.TestCaseRevision==testCase.Revision&&buildsContainingLastCommit.Count(b=>b.BuildNumber==tcr.BuildNumber)>0);
if(testCaseResult!=null)
从testCaseResult(testCaseResult、iterationId、actionId)返回GetActionScreenShot;
返回null;
}

api有用吗?谢谢,但我已经知道这些api,我正在寻找比它更复杂的东西(我也是在TFS 2018上)。很高兴看到您解决了您的问题。您可以,这对阅读此帖子的其他社区成员是有益的。