1

我们需要一种方法来访问由 CI 服务器(TFS 2013)上的 vstest 测试运行程序生成的 trx 文件,以便我可以在使用 Pickles(一种读取 trx 的工具)生成活文档(通过示例样式指定)之前向其中注入一些缺失的元素并输出一个html测试结果文件。)。我不知道该怎么做。我尝试使用 .runsettings 文件并以多种方式更改构建定义,但还没有运气。每次尝试,测试结果文件夹都是空的,其中没有 trx 文件。我可以在我的本地机器上做到这一点,如下所示

vstest.console.exe  myTestFile.dll /Settings:Local.RunSettings /InIsolation /TestCaseFilter:"Priority=1" /Logger:trx

但无法弄清楚如何在 CI 服务器中执行此操作,因为构建定义仅允许指定测试 dll 文件,无法指定 /Logger:trx 等开关。任何可行的方法都受到高度赞赏。

4

1 回答 1

1

经过一番研究,我发现 TFS2013 不会将其写入给定位置,而是将 TRX 文件存储在 TFS DB 中。您可以使用 TFS API 下载它。下面的代码将最新的 TRX 文件作为 XDocument 返回。但是,如果您想写入 CI 服务器中的已知位置,请使用第 78 行中的以下代码,而不是返回 XDocument。

latestRun.Attachments[0].DownloadToFile("C:\myxml.trx");

这是我的 TFS Service Wrapper 类,它从 TFS 2013 获取最新的 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-17T02:05:10.277 回答