In TFS API, how do I get the full class name for a given test?
.net, c#, tfs
Solution
You can do this by downloading the TRX file from TFS and parsing it manually. To download the TRX file for a test run, do this:
TfsTeamProjectCollection tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://my-tfs:8080/tfs/DefaultCollection"));
ITestManagementService tms = tpc.GetService<ITestManagementService>();
ITestManagementTeamProject tmtp = tms.GetTeamProject("My Project");
ITestRunHelper testRunHelper = tmtp.TestRuns;
IEnumerable<ITestRun> testRuns = testRunHelper.ByBuild(new Uri("vstfs:///Build/Build/123456"));
var failedRuns = testRuns.Where(run => run.QueryResultsByOutcome(TestOutcome.Failed).Any()).ToList();
failedRuns.First().Attachments[0].DownloadToFile(@"D:\temp\myfile.trx");
Then parse the TRX file (which is XML), looking for the <TestMethod> element, which contains the fully-qualified class name in the "className" attribute:
<TestMethod codeBase="C:/Builds/My.Test.AssemblyName.DLL" adapterTypeName="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" className="My.Test.ClassName, My.Test.AssemblyName, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Test_Method" />
Problem
I have an `ITestCaseResult` object in hand and I can't figure out how to extract the Test Class information from it. The object contains the test method's name in the `TestCaseTitle` property but there are a lot of duplicate titles across our code base and I would like more information. Assuming I have `Foo.Bar` assembly with class `Baz` and method `ThisIsATestMethod`, I currently only have access to the `ThisIsATestMethod` information from the title, but I would like to obtain `Foo.Bar.Baz.ThisIsATestMethod`. How can I do that using the TFS API? Here's some stripped down code: ``` var def = buildServer.CreateBuildDetailSpec(teamProject.Name); def.MaxBuildsPerDefinition = 1; def.QueryOrder = BuildQueryOrder.FinishTimeDescending; def.DefinitionSpec.Name = buildDefinition.Name; def.Status = BuildStatus.Failed | BuildStatus.PartiallySucceeded | BuildStatus.Succeeded; var build = buildServer.QueryBuilds(def).Builds.SingleOrDefault(); if (build == null) return; var testRun = tms.GetTeamProject(teamProject.Name).TestRuns.ByBuild(build.Uri).SingleOrDefault(); if (testRun == null) return; foreach (var outcome in new[] { TestOutcome.Error, TestOutcome.Failed, TestOutcome.Inconclusive, TestOutcome.Timeout, TestOutcome.Warning }) ProcessTestResults(bd, testRun, outcome); ``` ... ``` private void ProcessTestResults(ADBM.BuildDefinition bd, ITestRun testRun, TestOutcome outcome) { var results = testRun.QueryResultsByOutcome(outcome); if (results.Count == 0) return; var testResults = from r in results // The "r" in here is an ITestCaseResult. r.GetTestCase() is always null. select new ADBM.Test() { Title = r.TestCaseTitle, Outcome = outcome.ToString(), ErrorMessage = r.ErrorMessage }; } ```