0

I need to query and get the test results trx file from TFS 2013 so that I can modify that and use it for report generation. I couldn't find any method in IBuildServer that allows me to query the trx file. Is it posible to read the file using BuildDetail.DropLocation where I have the string that tells me the location of trx file. I have written few lines as below

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

2 回答 2

2

您可以使用以下代码读取 .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);
                }
            }
于 2015-09-15T06:45:53.307 回答
1

想通了。我添加了一个 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);
            }
        }

    }
}
于 2015-09-16T22:56:09.880 回答