Getting Test Settings from Microsoft Test Manager into your tests


This code is by way of the awesome Ben Schaefer. This should have been a native function included with Microsoft Test Manager. This code will allow your tests to retrieve environment speficic settings from Microsoft Test Manager. It's got the ability to create arbitrary settings in MTM, but tests can't access those settings without something like this.

From Ben:

For example the following will return the name of all machines in the environment your test is running in that have the Tag Value “TFE” added to them.

            EnvironmentConfig EnvCfg = new EnvironmentConfig();

            EnvironmentConfig.Agent[] TFEs = EnvCfg.getAgentsWithTagValue("TFE");

 

 

Setting the TFE Tag on your Agent can be done by going into your Lab Center, opening an environment and adding a new Tag or you can add attribute Tags directly to your environment and access them like this:

 

            EnvironmentConfig EnvCfg = new EnvironmentConfig();

            string tfeServer = EnvCfg.GetEnvironmentTagValue("TFE");

 

 

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using Microsoft.Win32;

using System.Xml;

using System.Xml.Serialization;

 

namespace Microsoft.BingMobile.Test.VirtualEarth.IPS

{

    public class EnvironmentConfig

    {

        public EnvironmentConfiguration TestEnvironmentConfig;

 

        public EnvironmentConfig(string ControllerConfigXmlFilePath)

        {

            XmlSerializer serializer = new XmlSerializer(typeof(EnvironmentConfiguration));

 

            using (XmlReader reader = XmlReader.Create(ControllerConfigXmlFilePath))

            {

                TestEnvironmentConfig = (EnvironmentConfiguration)serializer.Deserialize(reader);

            }

        }

 

        public EnvironmentConfig()

        {

            XmlSerializer serializer = new XmlSerializer(typeof(EnvironmentConfiguration));

 

            using (XmlReader reader = XmlReader.Create(GetConfigPath()))

            {

                TestEnvironmentConfig = (EnvironmentConfiguration)serializer.Deserialize(reader);

            }

        }

 

        public TestEnvironment[] getEnvironmentWithTagName(string tagName)

        {

            Dictionary<string, TestEnvironment> dictEnvironments = new Dictionary<string, TestEnvironment>();

 

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (Tag tag in testEnvironment.Tags)

                {

                    if (string.Compare(tagName, tag.name, true) == 0)

                    {

                        if (!dictEnvironments.ContainsKey(testEnvironment.name))

                        {

                            dictEnvironments.Add(testEnvironment.name, testEnvironment);

                        }

                    }

                }

            }

 

            return dictEnvironments.Values.ToArray();

        }

 

        public string GetEnvironmentTagValue(string tagName)

        {

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (Tag tag in testEnvironment.Tags)

                {

                    if (string.Compare(tagName, tag.name, true) == 0)

                        return tag.value;

                }

            }

 

            return string.Empty;

        }

 

        public Tag[] getEnvironmentTagsByAgentName()

        {

            return getEnvironmentTagsByAgentName(Environment.MachineName);

        }

 

        public Tag[] getEnvironmentTagsByAgentName(string agentName)

        {

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (MachineRole machineRole in testEnvironment.MachineRoles)

                {

                    foreach (Agent agent in machineRole.Agents)

                    {

                        if (string.Compare(agent.name, agentName, true) == 0)

                        {

                            return testEnvironment.Tags;

                        }

                    }

                }

            }

 

            return new Tag[0];

        }

 

        public Tag[] getEnvironmentTagsByEnvironmentName()

        {

            return getEnvironmentTagsByAgentName(Environment.MachineName);

        }

 

        public Tag[] getEnvironmentTagsByEnvironmentName(string environmentName)

        {

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                if (string.Compare(testEnvironment.name, environmentName, true) == 0)

                {

                    return testEnvironment.Tags;

                }

            }

 

            return new Tag[0];

        }

 

        public TestEnvironment[] getEnvironmentWithTagValue(string tagValue)

        {

            Dictionary<string, TestEnvironment> dictEnvironments = new Dictionary<string, TestEnvironment>();

 

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (Tag tag in testEnvironment.Tags)

                {

                    if (string.Compare(tagValue, tag.value, true) == 0)

                    {

                        if (!dictEnvironments.ContainsKey(testEnvironment.name))

                        {

                            dictEnvironments.Add(testEnvironment.name, testEnvironment);

                        }

                    }

                }

            }

 

            return dictEnvironments.Values.ToArray();

        }

 

        public Agent[] getAgentsWithTagName(string tagName)

        {

            Dictionary<string, Agent> dictAgents = new Dictionary<string, Agent>();

 

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (MachineRole machineRole in testEnvironment.MachineRoles)

                {

                    foreach (Agent agent in machineRole.Agents)

                    {

                        foreach (Tag tag in agent.Tags)

                        {

                            if (string.Compare(tag.name, tagName, true) == 0)

                            {

                                if (!dictAgents.ContainsKey(agent.name))

                                {

                                    dictAgents.Add(agent.name, agent);

                                }

                            }

                        }

                    }

                }

            }

 

            return dictAgents.Values.ToArray();

        }

 

        public Agent[] getAgentsWithTagValue(string tagValue)

        {

            Dictionary<string, Agent> dictAgents = new Dictionary<string, Agent>();

 

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (MachineRole machineRole in testEnvironment.MachineRoles)

                {

                    foreach (Agent agent in machineRole.Agents)

                    {

                        foreach (Tag tag in agent.Tags)

                        {

                            if (string.Compare(tag.value, tagValue, true) == 0)

                            {

                                if (!dictAgents.ContainsKey(agent.name))

                                {

                                    dictAgents.Add(agent.name, agent);

                                }

                            }

                        }

                    }

                }

            }

 

            return dictAgents.Values.ToArray();

        }

 

        public string getMachineTagValue(string tagName)

        {

            return getMachineTagValue(tagName, Environment.MachineName);

        }

 

        public string getMachineTagValue(string tagName, string machineName)

        {

            Tag[] tags = getMachineTags(machineName);

 

            foreach (Tag attribute in tags)

            {

                if (string.Compare(attribute.name, tagName, true) == 0)

                {

                    return attribute.value;

                }

            }

 

            return String.Empty;

        }

 

 

        public Tag[] getMachineTags()

        {

            return getMachineTags(Environment.MachineName);

        }

 

        public Tag[] getMachineTags(string machineName)

        {

            foreach (TestEnvironment testEnvironment in TestEnvironmentConfig.TestEnvironments)

            {

                foreach (MachineRole machineRole in testEnvironment.MachineRoles)

                {

                    foreach (Agent agent in machineRole.Agents)

                    {

                        if (string.Compare(agent.name, machineName, true) == 0)

                            return agent.Tags;

                    }

                }

            }

 

            return new Tag[0];

        }

 

        private string GetConfigPath()

        {

            //read the local registry to find the Test Controller Machine name

            RegistryKey VSAgentRegKey = Registry.LocalMachine;

            VSAgentRegKey = VSAgentRegKey.OpenSubKey("SOFTWARE\\MICROSOFT\\VisualStudio\\10.0\\EnterpriseTools\\QualityTools\\Agent");

 

            if (VSAgentRegKey == null)

                throw new Exception(@"The VS10 Test Agent registry key was not found on this machine.  Is the Agent Installed? If you are running this class on your Dev Box try the constructer for EnvironmentConfig with the XML path: \\[controller name]\c$\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTControllerConfig.xml ");

 

            string controllerName = (string)VSAgentRegKey.GetValue("ControllerMachineName");

            controllerName = controllerName.Split(':')[0];

 

            return @"\\" + controllerName + @"\c$\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTControllerConfig.xml";

        }

 

 

        [XmlRootAttribute("ControllerConfiguration", Namespace = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010/QTControllerConfig.xsd", IsNullable = false)]

        public class EnvironmentConfiguration

        {

            [XmlArray("TestEnvironments")]

            [XmlArrayItem("TestEnvironment")]

            public TestEnvironment[] TestEnvironments = new TestEnvironment[0];

 

            [XmlAttribute("tfsUri")]

            public string tfsUri = string.Empty;

 

            [XmlAttribute("tfsProjectCollectionId")]

            public string tfsProjectCollectionId = string.Empty;

        }

 

        public class TestEnvironment

        {

            [XmlAttribute("name")]

            public string name = string.Empty;

 

            [XmlAttribute("id")]

            public string id = string.Empty;

 

            [XmlElement("Description")]

            public string Description = string.Empty;

 

            [XmlArray("MachineRoles")]

            [XmlArrayItem("MachineRole")]

            public MachineRole[] MachineRoles = new MachineRole[0];

 

            [XmlArray("Properties")]

            [XmlArrayItem("Property")]

            public Tag[] Tags = new Tag[0];

        }

 

        public class MachineRole

        {

            [XmlArray("Agents")]

            [XmlArrayItem("Agent")]

            public Agent[] Agents = new Agent[0];

 

            [XmlAttribute("name")]

            public string name = string.Empty;

 

            [XmlAttribute("id")]

            public string id = string.Empty;

        }

 

        public class Agent

        {

            [XmlArray("Properties")]

            [XmlArrayItem("Attribute")]

            public Tag[] Tags = new Tag[0];

 

            [XmlAttribute("name")]

            public string name = string.Empty;

        }

 

        public class Tag

        {

            [XmlAttribute("name")]

            public string name = string.Empty;

 

            [XmlAttribute("value")]

            public string value = string.Empty;

        }

    }

}

 

 

 

 


Comments (1)

  1. bharath says:

    Hi,

    In hyderabad we have different institutes for training of software testing but if you want to go for good institue i will suggest for reliance global services. it is located in ameerpet lane opp to chandana brothers. for more details call them on 040-66666165/156. they are authorised training partners of QAI. so they are providing software testing with certifications,

    <html>

    <body>

    <a href="http://www.relianceglobal.com/SoftwareTraining.htm">Software testing training in hyderabad|Bangalore|Chennai</a>

    </body>

    </html>

Skip to main content