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

How to get the test results trx file(generated from vstest) from TFS Api

我需要从 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();

您可以使用下面的代码读取.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());
            }
        }

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

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);
            }
        }

    }
}