Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Creating Unit Tests with C#

Creating Unit Tests using C# and Visual Studio

Creating C4ShopDataValidation Library project for product data validation

Steps:

1. Open Visual Studio.

2. On the File menu=> New=>click Project.

3. New Project dialog box appears.

4. Installed Templates=> click Visual C#.

5. In the list of application types, click Class Library.

6. In Name box, type “C4ShopDataValidation” and then click OK.

Example:

Sample methods under test

   public bool ValidateProductame(ProductProduct)
        {
            if (string.IsNullOrEmpty(Product.ProductName))
                returnfalse;
 
            returntrue;
        }
 

 

public bool ValidatePrductNameWithSpecialChar(Product Product,out string ErrorMessage)
        {
            var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
            if (regexItem.IsMatch(Product.ProductName))
            {
                ErrorMessage = "Special characters not allowed";
                return false;
            }
            ErrorMessage = "";
            return true;
        }

 

Create C4Test Project for unit testing:

Steps:

1.  File menu=>Add=> New Project .

2.  New Project dialog box=> expand Installed=>expand Visual C#=>Test.

3.  Appears list of templates=>select Unit Test Project.

4.  In the Name box, enter BankTest, and then choose OK.

5.  Provide nem “C4Test” now project is added to the the C4Shop solution.

6.  In the C4Test project, add required references i.e  C4ShopDataValidation,BusnessEntities.

7.  Rename existing Class Class.1 or Add new if you want

8.  Start writing test cases.

Example:

 Unit test methods for product name validation   


   [TestClass]
    public class ProductDataValidationTests
    {
 
       //Declaration
        private IProductDataValidation _ProductValidation;
 
 
        //initalize
        [TestInitialize]
        public void ProductDataValidationInitialise()
        {
       
            _ProductValidation = new ProductDataValidation();
 
        }
 
 
        #region Sad Path
 
       
        [TestMethod]
        public void ChaekEmptyProductName()
        {
            var target = _ProductValidation;
            var product = new Product()
            {
                ProductName = "",
                Price = 115,
                Category = new ProductCategory()
              {
                  CategoryName = "Book"
              },
                ImageUrl = @"C:/data"
 
            };
            Assert.IsFalse(target.ValidateProductame(product));
 
        }
 
        [TestMethod]
        public void ChaekBadProductNameForSpecialChar()
        {
            string ExpectedErroMessage = "Special characters not allowed";
            string TargetMessage;
            var target = _ProductValidation;
            var product = new Product()
            {
                ProductName = "CodeChef4u ) SQL rcipies",
                Price = 500,
                Category = new ProductCategory()
                {
                    CategoryName = "Book"
                },
                ImageUrl = @"C:/data"
 
            };
            var isFailed=target.ValidatePrductNameWithSpecialChar(product,out TargetMessage);
            Assert.AreEqual(ExpectedErroMessage, TargetMessage);
        }
    }