I need to get obtain information from Test Cases that are only associated with Parent Work Items that have a particular FieldDefinition value.
What I've been trying to do, is get a list of projects from the Test Management Service, iterating through all the Test Cases, and from there, hopefully, find the Parent Work Item associated with the Test Case and see if it has the FieldDefinition value I'm looking for. Here is what I have so far, but once I get my hands on the Test Cases, I don't know where to go from there to A) get the Parent Work Item and B) query the FieldDefinitions to find the FieldDefinition value I'm looking for. The documentation is pretty murky:
public class TfsTest
{
private TfsTeamProjectCollection teamProjectCollection;
private WorkItemStore workItemStore;
private delegate void Execute();
public void GenerateReport()
{
TeamProjectPicker teamProjectPicker = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
teamProjectPicker.ShowDialog();
if (teamProjectPicker.SelectedTeamProjectCollection != null)
{
this.teamProjectCollection = teamProjectPicker.SelectedTeamProjectCollection;
ITestManagementService testManagementService = (ITestManagementService)this.teamProjectCollection.GetService(typeof(ITestManagementService));
this.workItemStore = (WorkItemStore)teamProjectCollection.GetService(typeof(WorkItemStore));
foreach (Project project in workItemStore.Projects)
{
ITestManagementTeamProject _testproject = testManagementService.GetTeamProject(project.Name);
GetTestPlans(_testproject);
}
}
}
void GetTestPlans(ITestManagementTeamProject testProject)
{
ITestPlanCollection plans = testProject.TestPlans.Query("Select * From TestPlan");
foreach (ITestPlan plan in plans)
{
var planName = plan.Name;
var testPlanType = ItemTypes.TestPlan;
Console.WriteLine(string.Format("* {0} ({1})", plan.Name, testPlanType));
if (plan.RootSuite != null && plan.RootSuite.Entries.Count > 0)
{
GetPlanSuites(plan.RootSuite.Entries, testProject);
}
}
}
void GetPlanSuites(ITestSuiteEntryCollection suites, ITestManagementTeamProject testProject)
{
foreach (ITestSuiteEntry suite_entry in suites)
{
IStaticTestSuite suite = suite_entry.TestSuite as IStaticTestSuite;
if (suite != null)
{
// Test suites...
var suiteTitle = suite.Title;
var testSuiteType = ItemTypes.TestSuite;
Console.WriteLine(string.Format("\t- {0} ({1})", suiteTitle, testSuiteType));
GetTestCases(suite, testProject);
}
}
}
void GetTestCases(IStaticTestSuite suite, ITestManagementTeamProject testProject)
{
// iterate each test case
foreach (ITestSuiteEntry suiteEntry in suite.TestCases)
{
if (suiteEntry.Id == 73649)
{
Console.WriteLine("Found it!");
}
var testResults = testProject.TestResults.ByTestId(suiteEntry.TestCase.Id);
// iterate each result for the case
foreach (ITestCaseResult result in testResults)
{
for (int actionIndex = 0; actionIndex < suiteEntry.TestCase.Actions.Count; actionIndex++)
{
var action = suiteEntry.TestCase.Actions[actionIndex];
if (!(action is ITestStep))
{
continue;
}
var step = action as ITestStep;
var topIteration = result.Iterations.FirstOrDefault();
if (topIteration == null)
{
continue;
}
var actionCount = topIteration.Actions.Count();
if (actionCount == 0 || actionIndex > (actionCount - 1))
{
continue;
}
var actionResult = topIteration.Actions[actionIndex];
string comment = actionResult.Comment;
var testCaseItemType = ItemTypes.TestCase;
// TODO: Find the parent and query
// Parent.Store.FieldDefinitions for
// FieldDefinigion.Name.Contains("Client(s)"))
// Where value == "OHIO"
Console.WriteLine(string.Format("\t\t- Action: {0} ({1})", step.Title, testCaseItemType));
Console.WriteLine(string.Format("\t\t- Expected Result: {0}", step.ExpectedResult));
foreach (var attachment in actionResult.Attachments)
{
//attachment.DownloadToFile(Path.Combine("C:\attachments", attachment.Name));
}
}
}
}
}
}
var tfsTest = new TfsTest();
tfsTest.GenerateReport();
Console.WriteLine();
Console.WriteLine("Done.");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();