我需要查询并从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();
答案 0 :(得分: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());
}
}
请注意,您需要选择用自己的buildUri替换vstfs:/// Build / Build / 531。要获得它,请使用以下代码:
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);
}
}
答案 1 :(得分: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);
}
}
}
}