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 };
}
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:
Then parse the TRX file (which is XML), looking for the <TestMethod> element, which contains the fully-qualified class name in the "className" attribute:
Here you have a way to get the Assembly name:
Unfortunately, ITestCaseResult and ITmiTestImplementation don’t seem to contain the namespace of the test case.
Check the last response in this link, that might help. Good Luck!
EDIT: This is based on Charles Crain's answer, but getting the class name without having to download to file:
And the method itself:
Since the details of the testcase are stored in the work item you can fetch the data by accessing the work item for the test case