TFS API Part 26 – Add/Remove Test Plans

30/06/2010

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.

image

For this task I had to use TFS 2010 API and especially MTM API in order to add Test Plan programmatically.

Download Demo Project

image 

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
Add comment
facebook linkedin twitter email

Leave a Reply

4 comments

  1. Daniel12/11/2010 ב 21:42

    I looked at this solution along with part 27, but I’m having trouble copying a test plan from one server to another.

    After attempting, I get an invalidstructurepathexception exception.

    I have searched far and wide and you seem to be the only one addressing issues like this for MTM. Thanks in advance.

  2. shair18/11/2010 ב 01:40

    There is no direct method to copy plan between servers.

  3. Prashant D25/02/2011 ב 12:56

    How to add each Test Step in Test Case?

  4. Avi19/07/2011 ב 04:36

    I have a very basic doubt here??
    ITestPlanCollection plans = _testproject.TestPlans.Query(“Select * From TestPlan”);

    in the above statement _testproject is variable of the interface ITestManagementTeamProject and
    TestPlans property gets the ITestPlanHelper interface.
    How does the interface ITestPlanHelper have a implementation for the menthod Query(string)??
    No method of an interface should have an implementation right?? How is this working??