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

Writing unit tests for happy, sad and bad path.

Writing unit tests for Happy, Sad and bad path.

In this article i will explain writing unit test cases that cover negative and positive scenarios to test data.

Unit test case that covers either happy path or sad path , what is this new term bad path ; check last example 

you will get better idea.

Happy path test Method:

A test case which results you in a positive result is a happy path.
Entering proper email address, username is valid data.

Example:

C# validation Code:

public bool ValidateProductame(Product Product)
        {
            if (string.IsNullOrEmpty(Product.ProductName))
                return false;
            return true;
        }

 

Test Method:

        /// <summary>
        /// validates Product name. 
        /// Result is valid Product Name.
        /// </summary>
        [TestMethod]
        public void ChaekValidProductName()
        {
            var target = _ProductValidation;
            var product = new Product()
            {
                ProductName = "CodeChef4u SQL rcipies",
                Price = 500,
                Category = new ProductCategory()
                {
                    CategoryName = "Book"
                },
                ImageUrl = @"C:/data"
 
            };
            Assert.IsTrue(target.ValidateProductame(product));
        }

 

Sad path unit test Method:

A test case which gives expected results for invalid data is sad path test method.
For example entering empty or null username.

C# validation Code:

public bool ValidateProductame(Product Product)
        {
            if (string.IsNullOrEmpty(Product.ProductName))
                return false;
            return true;
        }

 

Test method:

        /// <summary>
        /// validates Product name. 
        /// Result is invalid Product Name.
        /// </summary>
        [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));
 
        }

Bad path unit test Method:

With this you can test entering some invalid data A test case which results you some
Valid message you are expecting.

C# validation code:

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;
        }


Test method:

/// <summary>
        /// Validates Product name. 
        /// Result is valid Product Name.
        /// </summary>
        [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);
        }

Writing Unit tests in C# with moq

Writing Unit testing in C# with moq

What is Unit testing?

Wikipedia definition
Unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.

Reference: https://en.wikipedia.org/wiki/Unit_testing

What is Moq?

Moq is the mocking library for .NET developed that take full advantage of .NET 3.5 (i.e. Linq expression trees) and C# 3.0 features (i.e. lambda expressions) that make it the most productive, type-safe and refactoring-friendly mocking library available. And it supports mocking interfaces as well as classes.

Installing Moq:

You can download Moq from GitHub and add the appropriate references to your project, or you can install it using nugget package manager.



Unit testing in VS 2013 with Moq:

I want to test validation business logic for my finance web application following business entities I am using for product data.

  public class Product
   {
      public int ProductId { get; set; }
 
      public string ProductName { get; set; }
 
      public decimal Price { get; set; }
 
      public ProductCategory Category { get; set; }
 
      public string ImageUrl { get; set; }
   }

Product data validation logic and used interfaces for notication:

namespace C4RandDWebApp
{
    public class ProductDataValidation
    {
        private readonly ILogger _logger;
        private readonly IEmailer _emailer;
 
        public ProductDataValidation(ILogger Logger, IEmailer Emailer)
        {
            _logger = Logger;
            _emailer = Emailer;
        }
 
 
        public bool ValidateProductame(Product Product)
        {
            if (string.IsNullOrEmpty(Product.ProductName))
                return false;
           {
                     _logger.LogWarning("Invalid username" + Product.ProductName);
                    // Email internet dev
                    _emailer.SendWarningEmail(Product.ProductName,
                        "Invalid username");
          }
            return true;
        }

        public bool ValidateProductPrice(Product Product)
        {
            if (Product.Price == null || Product.Price == 0M)
                return false;
            return true;
        }

        public bool ValidateProductCategory(Product Product)
        {
            if (string.IsNullOrEmpty(Product.Category.CategoryName))
                return false;
            return true;
        }
    }
}
 


Created unit testing project named C4Testing and added  all required dll reference to project. 

Created following test class inside project that unit tests all scenarios for my application product data validation. 

Unit testing Class with Moq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using C4RandDWebApp;
 
namespace C4Testing
{
    /// <summary>
    ///This is a test class for Product validation and is intended
    ///to contain all Product validation Unit Tests
    ///</summary>
    [TestClass]
    class ProductDataValidationTests
    {
        // Mocks
        private Mock<ILogger> _mockLogger;
        private Mock<IEmailer> _mockEmailer;
 
       //Declaration
        private IProductDataValidation _ProductValidation;
 
 
        //initalize
        [TestInitialize]
        public void ProductValiatotInitialise()
        {
            _mockLogger = new Mock<ILogger>();
            _mockEmailer = new Mock<IEmailer>();
            _ProductValidation = new C4RandDWebApp.ProductDataValidation(_mockLogger.Object, _mockEmailer.Object);
 
        }
 
 
        #region Sad Path
 
        /// <summary>
        /// Validates Product name. 
        /// Result is invalid Product Name.
        /// </summary>
        [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));
 
        }
 
        #endregion
 
 
        #region Happy Path
        /// <summary>
        /// validates Product name. 
        /// Result is valid Product Name.
        /// </summary>
        [TestMethod]
        public void ChaekValidProductName()
        {
            var target = _ProductValidation;
            var product = new Product()
            {
                ProductName = "CodeChef4u SQL rcipies",
                Price = 500,
                Category = new ProductCategory()
                {
                    CategoryName = "Book"
                },
                ImageUrl = @"C:/data"
 
            };
            Assert.IsTrue(target.ValidateProductame(product));
 
        }
        #endregion
    }
 

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);
        }
    }