使用 vstest 作为测试运行器将 trx 文件写入 TFS(CI 服务器)上的已知位置

Write the trx file to a known location on TFS(CI server) using vstest as the test runner

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

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

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

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

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

这是我的 TFS 服务包装器 class,它从 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);
            }
        }

    }
}