Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic

Manage TFS Test Suites in Microsoft Test Manager C# VB.NET Code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
21 Feb 2015CPOL2 min read 12.1K   12  
Manage TFS Test Suites in Microsoft Test Manager C# VB.NET code

Introduction

In order to use TFS Test Suites via code, you should first create TFS Collection and Team Test Project. You can find more information in my previous post: “How to: Connect to TFS Team Project C# Visual Basic .NET Code“.

Next, you need to connect to specific test plan. You can read more in the article: “Manage TFS Test Plans in MS Test Manager C# VB .NET Code“.

In MS Test Manager, you can edit, create, delete test suites. Here, I will show you how to do these actions via C# and Visual Basic .NET code.

There are three types of test suites- static, requirement-based and query-based. You can add child suites only to the first type. The current tutorial is only applicable for the static test suites.

Get All TFS Test Suites

Our first job is to create a wrapper class for the TFS Suite (ITestSuiteBase). It will contain only the most important properties of the base core TFS suite object and will be serializable because the standard MS objects are not. This means that you cannot use them in web services or put them into the clipboard.

C#
[Serializable]
    public class Suite : IComparable<Suite>
    {    
        public Suite Parent { get; set; }

        public int Id { get; set; }

        public List<Suite> SubSuites { get; set; }

        public string Title { get; set; }

        public int CompareTo(Suite other)
        {
            return this.Title.CompareTo(other.Title);
        }
    }

You need the following three methods to get a tree based list of all static test suites in the specified test plan.

C#
public static List<ITestSuiteBase> GetAllTestSuitesInTestPlan(ITestPlan testPlan)
        {
            List<ITestSuiteBase> testSuites = GetAllTestSuites(testPlan.RootSuite.SubSuites);
            return testSuites;
        }

        public static List<Suite> GetAllSuites(ITestSuiteCollection subSuitesCore)
        {
            List<Suite> subSuites = new List<Suite>();
            foreach (ITestSuiteBase currentSuite in subSuitesCore)
            {
                if (currentSuite != null)
                {
                    currentSuite.Refresh();
                    List<Suite> childred = null;
                    if (currentSuite is IStaticTestSuite)
                    {
                        IStaticTestSuite suite = currentSuite as IStaticTestSuite;
                        if (suite.SubSuites != null && suite.SubSuites.Count > 0)
                        {
                            childred = GetAllSuites(suite.SubSuites);
                        }
                    }
                    Suite newSuite = new Suite(currentSuite.Title, currentSuite.Id, childred);

                    SetParentToAllChildrenSuites(childred, newSuite);
                 
                    subSuites.Add(newSuite);
                }
            }

            return subSuites;
        }

        public static void SetParentToAllChildrenSuites(List<Suite> childred, Suite newSuite)
        {
            if (childred != null)
            {
                List<Suite> currentChildrens = childred.ToList();
                currentChildrens.Sort();
                foreach (Suite currentChild in childred)
                {
                    currentChild.Parent = newSuite;
                }
            }
        }

testPlan.RootSuite.SubSuites gives you all first level test suites in the test plan. Next, we use recursion to get all child test suites. You need to refresh every item because otherwise you will not get the last screenshot of the test suites, TFS sometimes caches the query results. Every custom suite entity contains the list of its childs and reference to its parent.

Add New TFS Test Suite

C#
public static int AddSuite(ITestManagementTeamProject testManagementTeamProject, 
	ITestPlan testPlan, int parentSuiteId, string title)
        {
            ITestSuiteBase parentSuite = null;
            if (parentSuiteId != -1)
            {
                parentSuite = testManagementTeamProject.TestSuites.Find(parentSuiteId);
            }

            if (parentSuite is IRequirementTestSuite)
            {
                return 0;
            }
            IStaticTestSuite staticSuite = testManagementTeamProject.TestSuites.CreateStatic();
            staticSuite.Title = title;

            if (parentSuite != null && parentSuite is IStaticTestSuite && parentSuiteId != -1)
            {
                IStaticTestSuite parentSuiteStatic = parentSuite as IStaticTestSuite;
                parentSuiteStatic.Entries.Add(staticSuite);
            }
            else
            {
                testPlan.RootSuite.Entries.Add(staticSuite);
            }
            testPlan.Save();

            return staticSuite.Id;
        }

First, we initialize the parent test suite, if it is not a static suite, we stop the process. If the parent suite is not found, we add the new test suite to the entries of the root test plan suite. In order to complete the process, we need to save the test plan.

Delete TFS Test Suite

C#
public static void DeleteSuite(ITestManagementTeamProject testManagementTeamProject, 
	ITestPlan testPlan, int suiteToBeRemovedId, IStaticTestSuite parent = null)
        {
            ITestSuiteBase currentSuite = testManagementTeamProject.TestSuites.Find(suiteToBeRemovedId);
            if (currentSuite == null)
            {
                throw new ArgumentNullException("The suite for deletion cannot be found!");
            }
            // Remove the parent child relation. This is the only way to delete the suite.
            if (parent != null)
            {
                parent.Entries.Remove(currentSuite);
            }
            else if (currentSuite.Parent != null)
            {
                currentSuite.Parent.Entries.Remove(currentSuite);
            }
            else
            {
                // If it's initial suite, remove it from the test plan.
                testPlan.RootSuite.Entries.Remove(currentSuite);
            }

            // Apply changes to the suites
            testPlan.Save();
        }

If the parent is not found, we remove the test suite from the test plan suites. Again, to complete the process, we need to save the test plan.

Create TFS Queries

C#
public static ITestSuiteBase GetTestSuiteByName
	(ITestManagementTeamProject testManagementTeamProject, string suiteName)
        {
            string query = string.Concat("SELECT * _
            FROM TestSuite where Title = '", suiteName, "'");
            var firstMatchingSuite = testManagementTeamProject.TestSuites.Query(query);

            return firstMatchingSuite.FirstOrDefault();
        }

So Far in the TFS API Series

1. Connect to TFS Team Project C# Code
2. Manage TFS Test Plans C# Code
3. Manage TFS Test Cases C# Code
4. Manage TFS Test Suites C# Code
5. TFS Associate Automated Test with Test Case C# Code
6. Test Cases Statistics with SSRS and TFS Data Warehouse
7. SSRS SQL Server Reporting Services- Subscriptions for Reports

 

If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!

Source Code

The post- Manage TFS Test Suites in MS Test Manager C# VB.NET Code appeared first on Automate The Planet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
-- There are no messages in this forum --