经过一番研究,我发现 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);
}
}
}
}