Setting up Unit Testing in Visual Studio C#

If you have a project with various classes and want to test some methods quickly, Microsoft provided Unit testing tools for this purpose. In this tutorial we are going to build simple class library and method that extracts value from xml. Then we are going to create separate project that will be used to test the method we just wrote. I am going to use C# as main language.

1. Create new class library in Visual Studio. I called it ABLibrary, but you can name it whatever you want, just use your name later in project reference.


2. Rename class to XMLHelpers.

public class XMLHelpers
{
    /// 
        /// Extract value from XML Node
        /// 
        ///         ///         /// 
        /// 
        public static string ExtractXMLNodeValueFromXML(string xml, string nodePath)
        {
            if (string.IsNullOrEmpty(nodePath))
                return string.Empty;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            XmlNode xmlNode = xmlDoc.SelectSingleNode(nodePath);

            if (xmlNode != null)
            {
                return xmlNode.InnerText;
            }
            else
            {
                return string.Empty;
            }
        }

} 

 


3. Add new project to your solution(class library again) and call it ABLibraryTest.


4. Add ABLibrary as reference and Microsoft.VisualStudio.QualityTools.UnitTestFramework. You find ABLibrary under Solution and Unit test tools in Assemblies.


5. Rename default class to XMLHelperTest (make the naming consistent) and create TestExtractValueFromXMLNode method.
[TestClass]
    public class XMLHelpersTest
    {
        public const string xmlTest = "Value1";

        [TestMethod]
        public void TestExtractValueFromXMLNode()
        {
            Assert.AreEqual("Value1", XMLHelpers.ExtractXMLNodeValueFromXML(xmlTest, "TEST/Node1"), "Extract failed or values do not match");
            Assert.AreEqual(string.Empty, XMLHelpers.ExtractXMLNodeValueFromXML(xmlTest, string.Empty), "Method fails, as it does expect empty string as path");
            Assert.AreEqual(string.Empty, XMLHelpers.ExtractXMLNodeValueFromXML(xmlTest, null), "Method fails, as it does expect null as path");
        }
    }



6. In order for visual studio to run tests we have to make test method and class public and add [TestClass] annotation on class and [TestMethod] on method.


7. Assert.AreEqual method in our case expect first parameter as expected value, actual value and returns message if test fail.


8. Press Ctrl+R then T to run the test and wait for the results.


9. Let's try to make our tests to fail while modifying the ExtractXMLNodeValueFromXML in XMLHelpers class. Change the method return if nodePath is empty string or null to return null.



        /// 
        /// Extract value from XML Node
        /// 
        ///         ///         /// 
        /// 
        public static string ExtractXMLNodeValueFromXML(string xml, string nodePath)
        {
            if (string.IsNullOrEmpty(nodePath))
                return null;

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);

            XmlNode xmlNode = xmlDoc.SelectSingleNode(nodePath);

            if (xmlNode != null)
            {
                return xmlNode.InnerText;
            }
            else
            {
                return string.Empty;
            }
        }

 


10. So as you can see you can use ExtractXMLNodeValueFromXML method in some various applications and if you want change the core functionality and your test fails it is a good chance that your application might fail as wll. Good test cases prevent(limits) runtime errors and overall adding more confident in the project as future maintenance and scalability is easier achievable.

Comments