Tuesday, April 12, 2011

How to create a unit test using Microsoft.VisualStudio.TestTools.UnitTesting in Visual Studio 2008 Professional Edition?

To be able to perform unit testing in Visual Studio 2008 Professional Edition, you need either visual studio 2008 professional or team system edition.
Step 1 – Right click your solution then click on Add then click on new project.
Step 2 – Type your chosen name of your test project then select the location of your test project will be.
Step 3 - Right Click on your newly created test project then click on add then click on unit test.
Step 4 –  Choose the items that you wish to test. Just click on the checkbox beside each items. Then choose your output test project. Click on the newly created test project.
Step 5 – Just click on Yes. It will generate all your unit test on your chosen output test project.
Here is a sample code of unit test:
using MyDAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

namespace MyTestProject
{
   
   
    /// <summary>
    ///This is a test class for CitiesBLLTest and is intended
    ///to contain all CitiesBLLTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CitiesBLLTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for UpdateCity
        ///</summary>
        [TestMethod()]
        public void UpdateCityTest()
        {
            CitiesBLL target = new CitiesBLL(); // TODO: Initialize to an appropriate value
            Cities citys = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; }); // TODO: Initialize to an appropriate value
            Countries country = new Countries();
            CountriesBLL countryBll = new CountriesBLL();
            if (citys == null)
            {
                citys = new Cities();
                country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                if (country == null)
                {
                    country = new Countries();
                    country.CountryCode = "JP";
                    country.CountryName = "Japan";
                    country.ContinentID = 0;
                    countryBll.InsertCountry(country);
                    country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                }
                citys.CityCode = "TKY";
                citys.CityName = "Tokyo";
                citys.CountryID = country.CountryID;
                target.InsertCity(citys);
                citys = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; });
            }
            citys.CityCode = "MNL";
            citys.CityName = "Manila";             
            int expected = 1; // TODO: Initialize to an appropriate value
            int actual;
            if (target.CheckForDuplicateCity(citys, true) == false)
            {
                actual = target.UpdateCity(citys);
                Assert.AreEqual(expected, actual);
                Assert.IsTrue(expected == actual);
            }
            else
                Assert.IsTrue(true);  
        }

        /// <summary>
        ///A test for RetrieveCityList
        ///</summary>
        [TestMethod()]
        public void RetrieveCityListTest()
        {
            CitiesBLL target = new CitiesBLL(); // TODO: Initialize to an appropriate value
            List<Cities> expected = target.RetrieveCityList(); // TODO: Initialize to an appropriate value
            Cities city = new Cities();
            Countries country = new Countries();
            CountriesBLL countryBll = new CountriesBLL();
            if (expected == null)
            {
                expected = new List<Cities>();
                country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                if (country == null)
                {
                    country = new Countries();
                    country.CountryCode = "JP";
                    country.CountryName = "Japan";
                    country.ContinentID = 0;
                    countryBll.InsertCountry(country);
                    country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                }
                city.CityCode = "TKY";
                city.CityName = "Tokyo";
                city.CountryID = country.CountryID;
                target.InsertCity(city);
                expected = target.RetrieveCityList();
            }
            List<Cities> actual;
            actual = target.RetrieveCityList();
            Assert.IsTrue(expected.Count  == actual.Count );  
        }

        /// <summary>
        ///A test for RetrieveCityById
        ///</summary>
        [TestMethod()]
        public void RetrieveCityByIdTest()
        {
            CitiesBLL target = new CitiesBLL(); // TODO: Initialize to an appropriate value
            Cities expected = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; }); // TODO: Initialize to an appropriate value
            Countries country = new Countries();
            CountriesBLL countryBll = new CountriesBLL();
            if (expected == null)
            {
                expected = new Cities();
                country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                if (country == null)
                {
                    country = new Countries();
                    country.CountryCode = "JP";
                    country.CountryName = "Japan";
                    country.ContinentID = 0;
                    countryBll.InsertCountry(country);
                    country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                }
                expected.CityCode = "TKY";
                expected.CityName = "Tokyo";
                expected.CountryID = country.CountryID;
                target.InsertCity(expected);
                expected = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; });
            }
            int id = expected.CityID; // TODO: Initialize to an appropriate value
            Cities actual;
            actual = target.RetrieveCityById(id);
            Assert.AreEqual(expected, actual);
            Assert.IsTrue(expected == actual);  
        }

        /// <summary>
        ///A test for InsertCity
        ///</summary>
        [TestMethod()]
        public void InsertCityTest()
        {
            CitiesBLL target = new CitiesBLL(); // TODO: Initialize to an appropriate value
            Cities citys = new Cities(); // TODO: Initialize to an appropriate value
            Countries country = new Countries();
            CountriesBLL countryBll = new CountriesBLL();
            country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
            if (country == null)
            {
                country = new Countries();
                country.CountryCode = "JP";
                country.CountryName = "Japan";
                country.ContinentID = 0;
                countryBll.InsertCountry(country);
                country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
            }
            citys.CityCode = "TKY";
            citys.CityName = "Tokyo";
            citys.CountryID = country.CountryID;
            int expected = 1; // TODO: Initialize to an appropriate value
            int actual;
            if (target.CheckForDuplicateCity(citys, false) == false)
            {
                actual = target.InsertCity(citys);
                Assert.AreEqual(expected, actual);
                Assert.IsTrue(expected == actual);
            }
            else
                Assert.IsTrue(true);  
        }

        /// <summary>
        ///A test for DeleteCity
        ///</summary>
        [TestMethod()]
        public void DeleteCityTest()
        {
            CitiesBLL target = new CitiesBLL(); // TODO: Initialize to an appropriate value
            Cities city = new Cities();
            Countries country = new Countries();
            CountriesBLL countryBll = new CountriesBLL();
            city = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; });
            if (city == null)
            {
                city = new Cities();
                country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                if (country == null)
                {
                    country = new Countries();
                    country.CountryCode = "JP";
                    country.CountryName = "Japan";
                    country.ContinentID = 0;
                    countryBll.InsertCountry(country);
                    country = countryBll.RetrieveCountryList().Find(delegate(Countries c) { return c.CountryCode == "JP"; });
                }
                city.CityCode = "TKY";
                city.CityName = "Tokyo";
                city.CountryID = country.CountryID;
                target.InsertCity(city);
                city = target.RetrieveCityList().Find(delegate(Cities c) { return c.CityCode == "TKY"; });
            }
            int id = city.CityID; // TODO: Initialize to an appropriate value
            int expected = 1; // TODO: Initialize to an appropriate value
            int actual;
            actual = target.DeleteCity(id);
            Assert.AreEqual(expected, actual);
            Assert.IsTrue(expected == actual);  
        }

        /// <summary>
        ///A test for CitiesBLL Constructor
        ///</summary>
        [TestMethod()]
        public void CitiesBLLConstructorTest()
        {
            CitiesBLL target = new CitiesBLL();
        }
    }
}

Here are the other references that I have used:
http://www.dreamincode.net/forums/topic/108976-c%23-unit-testing-basics/
http://www.codeproject.com/KB/cs/autp1.aspx
http://msdn.microsoft.com/en-us/library/ms379625(v=vs.80).aspx

No comments:

Post a Comment