如何从TFS Api获得测试结果trx文件(从vstest生成)

本文关键字:文件 vstest 生成 trx TFS Api 测试结果 | 更新日期: 2023-09-27 18:12:24

我需要查询并从TFS 2013中获取测试结果trx文件,以便我可以修改该文件并将其用于报告生成。我在IBuildServer中找不到任何允许我查询trx文件的方法。是否有可能使用BuildDetail.DropLocation读取文件,其中我有字符串,告诉我trx文件的位置。我写了以下几行

TfsSerice = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsUri));
TfsSerice.EnsureAuthenticated();
var buildServer = (IBuildServer)TfsSerice.GetService(typeof(IBuildServer));
BuildDetail = buildServer.QueryBuilds("MyUserLibrary").FirstOrDefault();

如何从TFS Api获得测试结果trx文件(从vstest生成)

您可以使用下面的代码读取.trx测试结果文件:

TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://tfsservername:8080/tfs/DefaultCollection"));
        ITestManagementTeamProject project = tfs.GetService<ITestManagementService>().GetTeamProject("teamprojectname");
        foreach (ITestRun tRun in project.TestRuns.ByBuild(new Uri(("vstfs:///Build/Build/531"))))
        {
            foreach (ITestCaseResult tr in tRun.QueryResults())
            {
                Console.WriteLine(tr.TestCaseTitle.ToString() + ":" + tr.Outcome.ToString());
            }
        }

请注意,您需要选择用您自己的buildUri替换vstfs:///Build/Build/531。要获取它,使用以下代码:

IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
        var buildDefinitions = buildServer.QueryBuildDefinitions("TeamProject");

        foreach (var buildDefinition in buildDefinitions)
        {
            if (buildDefinition.Name == "BuildDefinitionName")
            {
                var builds = buildDefinition.QueryBuilds();
                foreach (var build in builds)
                {
                    Console.WriteLine(build.Uri);
                }
            }

我明白了。我添加了一个TFS服务包装器,如下所示,它捕获最新构建的TRX文件。下面是代码,享受!!!!

using System.Linq;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using System.Configuration;
using System;

namespace TfsService
{
    public class TfsServiceWrapper
    {
        public TfsTeamProjectCollection TeamProjectCollection { get; private set; }
        public string TeamProject { get; private set; }
        public string BuildName { get; private set; }
        public Uri TfsUri { get; private set; }
        public TfsServiceWrapper()
        {
            TfsUri = new Uri(ConfigurationManager.AppSettings["tfsUri"]);
            TeamProject = ConfigurationManager.AppSettings["teamProject"];
            BuildName = ConfigurationManager.AppSettings["buildName"];
            ConnectToTeamProjectCollection();
        }
        public TfsServiceWrapper(Uri tfsUri, string teamProject, string buildName)
        {
            TfsUri = tfsUri;
            TeamProject = teamProject;
            BuildName = buildName;
            ConnectToTeamProjectCollection();
        }
        private void ConnectToTeamProjectCollection()
        {
            TeamProjectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsUri);
            TeamProjectCollection.EnsureAuthenticated();
        }
        public IBuildDetail LatestBuildDetail
        {
            get
            {
                var spec = BuildServer.CreateBuildDetailSpec(TeamProject, BuildName);
                spec.MaxBuildsPerDefinition = 1;
                spec.QueryOrder = BuildQueryOrder.FinishTimeDescending;
                return BuildServer.QueryBuilds(spec).Builds.FirstOrDefault();
            }
        }
        public IBuildServer BuildServer
        {
            get
            {
                return (IBuildServer)TeamProjectCollection.GetService(typeof(IBuildServer));
            }
        }

        public ITestManagementService TestManagementService
        {
            get
            {
                return (ITestManagementService)TeamProjectCollection.GetService(typeof(ITestManagementService));
            }
        }
        public XDocument LatestTestResultFile
        {
            get
            {
                var latestRun = TestManagementService.GetTeamProject(TeamProject).TestRuns.ByBuild(LatestBuildDetail.Uri).First(run => run.QueryResults().Any());
                var resolver = new XmlUrlResolver {Credentials = CredentialCache.DefaultCredentials};
                var settings = new XmlReaderSettings {XmlResolver = resolver};
                var reader = XmlReader.Create(latestRun.Attachments[0].Uri.ToString(), settings);
                return XDocument.Load(reader);
            }
        }
    }
}