0

I am trying to unit test a simple function (compiles to a library) in c# VS 2012.

public static class Configuration
    {
        public static T DeSerialize<T>(string filePath)
        {
            if (!System.IO.File.Exists(filePath))
            {
                throw new System.IO.FileNotFoundException(filePath);
            }

            using (Stream reader = new FileStream(filePath, FileMode.Open))
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(T);
                return (T)serializer.Deserialize(reader);
            }
        }
    }

    [TestClass]
    public class Test
    {
        [TestMethod]
        public void deserializationTest()
        {
            var something = Configuration.DeSerialize<Item>(@"d:\CoffeeShop.Items.config");
            Console.WriteLine(something);

        }
    }

But, I am stuck up with what to do next?. What namespaces should I import. Any pointers is much appreciated.

1 Answers1

3

You should be using the Microsoft.VisualStudio.TestTools.UnitTesting namespace, which contains the MSTest framework classes (which you are using as implied by the TestClass and TestMethod attributes). You should then use the Assert class' methods to verify your test outcome. You code might look like this: `

[TestMethod]
public void deserializationTest()
{
    var something = Configuration.DeSerialize<Item>(@"d:\CoffeeShop.Items.config");
    Assert.AreEqual("expected item name", something.Name);
}

This test verifies that the DeSerialize call returns an Item which Name property is equal to "expected item name". Of course, this is just an assumption on how your Item class looks like, but you probably get the gist.

If you are getting started with unit testing in Visual Studio, this tutorial might come in handy: http://msdn.microsoft.com/en-us/library/ms182532.aspx

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • Thank you. But, i dont have a `Microsoft.visualStudio.TestTools.UnitTesting` dll in vs2012. It seems to have a `Microsoft.VisualStudio.TestTools.UITesting.dll`. Where can I download the above one ? – now he who must not be named. Jan 30 '14 at 08:39
  • The DLL is called: `Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll`. You can find more information here: http://stackoverflow.com/questions/3293317/where-is-the-microsoft-visualstudio-testtools-unittesting-namespace-on-vs2010 – Erik Schierboom Jan 30 '14 at 08:41
  • Thank you . That works. All I had to do was to go to the `C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll` and `reference it` to my project. right click the `[TestClass] attribute` and click `Debug Tests`. `VS2012` by itself does not seem to support this `dll`. – now he who must not be named. Jan 30 '14 at 08:50