TFS API Part 26 – Add/Remove Test Plans
TFS API Part 26 – Add/Remove Test Plans
Microsoft Test Manager is a great tool helping us to manage our Test Plan Test Suites and our Test Cases.
As you can see from the picture below you can define as many Test Plans as you want to manage your Testing cycles, Today I had a customer with a special need – he wanted to add Test Plan automatically after a successful Build, The name of the new Test Plan need to be the same as the Build name and all the related Requirements from the latest build need to be inside the Test Plan.

For this task I had to use TFS 2010 API and especially MTM API in order to add Test Plan programmatically.
Download Demo Project
Step 1: Create Project and Add Reference
Create an WPF/WinForm application and add the following references:
- Microsoft.TeamFoundation.WorkItemTracking.Client.dll
- Microsoft.TeamFoundation.Client.dll
- Microsoft.TeamFoundation.dll
- Microsoft.TeamFoundation.TestManagement.Client
All files located under - c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\
Step 2: Connect to Team Foundation Server
private TfsTeamProjectCollection _tfs;
private ITestManagementTeamProject _testproject;
TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
tpp.ShowDialog();
if (tpp.SelectedTeamProjectCollection != null)
{
this._tfs = tpp.SelectedTeamProjectCollection;
ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
this._testproject = test_service.GetTeamProject(tpp.SelectedProjects[0].Name);
GetTestPlans();
btn_add.IsEnabled = true;
btn_remove_plan.IsEnabled = true;
}
Step 3: Get Test Plans
To get test plan under specific project you will have to use ITestManagementTeamProject and perform Query.
ITestPlanCollection plans = _testproject.TestPlans.Query("Select * From TestPlan");
this.list_plans.ItemsSource = plans;
//_testproject.TestPlans.Find("int - If you know the id...");
Step 4: Add New Test Plan
ITestPlan plan = _testproject.TestPlans.Create();
plan.Name = txt_plan_name.Text;
//plan.AreaPath, plan.Description,plan.EndDate,plan.StartDate,plan.State
plan.Save();
txt_plan_name.Text = string.Empty;
GetTestPlans();
this.list_plans.SelectedItem = plan;
Step 5: Delete Test Plan
if (list_plans.SelectedItem != null)
{
ITestPlan plan = list_plans.SelectedItem as ITestPlan;
plan.Delete(DeleteAction.ForceDeletion);
GetTestPlans();
}
else
MessageBox.Show("Please select plan");
Full Code:
private void btn_connect_Click(object sender, RoutedEventArgs e)
{
TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false);
tpp.ShowDialog();
if (tpp.SelectedTeamProjectCollection != null)
{
this._tfs = tpp.SelectedTeamProjectCollection;
ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
this._testproject = test_service.GetTeamProject(tpp.SelectedProjects[0].Name);
GetTestPlans();
btn_add.IsEnabled = true;
btn_remove_plan.IsEnabled = true;
}
}
void GetTestPlans()
{
ITestPlanCollection plans = _testproject.TestPlans.Query("Select * From TestPlan");
this.list_plans.ItemsSource = plans;
//_testproject.TestPlans.Find("int - If you know the id...");
}
private void btn_add_Click(object sender, RoutedEventArgs e)
{
ITestPlan plan = _testproject.TestPlans.Create();
plan.Name = txt_plan_name.Text;
//plan.AreaPath, plan.Description,plan.EndDate,plan.StartDate,plan.State
plan.Save();
txt_plan_name.Text = string.Empty;
GetTestPlans();
this.list_plans.SelectedItem = plan;
}
private void btn_remove_plan_Click(object sender, RoutedEventArgs e)
{
if (list_plans.SelectedItem != null)
{
ITestPlan plan = list_plans.SelectedItem as ITestPlan;
plan.Delete(DeleteAction.ForceDeletion);
GetTestPlans();
}
else
MessageBox.Show("Please select plan");
}
Download Demo Project
Enjoy