Unit Testing With MS Tests In C#

Many junior/fresher developers might be confused about who really should deal with Unit Testing. Unit test cases are written by developers.

What is Unit Testing?

It tests behavior of a function/method by writing another piece of code.

Unit testing
Image 1: Unit testing

Why Unit Tests?

  • Normally, software projects are intended to run for long-term, at least for a minimum of five years.
  • During this period, maintaining the application is very crucial.
  • Any change request received might impact the other functionality of the application.
  • So, before deploying the change in the production, a lot of regression testing has to be done. This consumes a lot of tester’s time.

Imagine a situation where the change requests are happening very frequently. The efforts required for regression testing will be very high and the possibility of having defects will also be high.

Software Maintenance with Normal approach (Regression Testing)

Regression testing
Image 2: Regression Testing

Regression Testing is the process of testing changes to computer programs, in order to make sure that the older programming still works with the new changes.

Software Maintenance with Unit Testing

Unit tests
Image 3: Unit Tests

  • Unit Tests will certainly help in minimizing the Regression Testing.
  • Every method will be associated with Test Methods. Test Methods will test the purpose of the actual method.
  • Test Methods will check for the following scenarios:
    • Positive / Success scenario
    • Negative / Failure scenario
    • Exception / Error Handling scenario

  • A normal method might require more than one Test Method depending on the complexity of the Method.
  • Before code delivery, developer has to ensure that all the test methods (in the entire solution) are getting passed.

Software maintenance with TDD (Test Driven Development)

TDD
Image 4: TDD

TDD is an evolutionary approach to the development. It combines test-first development where you write a test before you write just enough production code to fulfill that test; and then, refactor the code to pass the test.

Writing Unit Test cases

We have two frameworks to write Unit Test cases in C#.

  • MS Test
  • NUnit

We have AAA pattern to write Unit Test cases:

AAA
Image 5: AAA

  1. Arrange all the necessary preconditions and inputs.
  2. Act on the object or method under test.
  3. Assert that the expected results have occurred.
Following are the steps to create the unit test project:

Right click on the solution explorer and click on Add and select Unit Test Project,

Test Project
Image 6: Test Project

Solution Explorer

Solution Explorer
Image7 : Solution Explorer

Test Class

 

  1. using System;  
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;  
  3. using BusinessManager;  
  4.   
  5. namespace UnitTestProject1  
  6. {  
  7.     [TestClass]  
  8.     public class UnitTest1  
  9.     {  
  10.         [TestMethod]  
  11.         public void GetNameTest()  
  12.         {  
  13.             //Arrange  
  14.             Employee objEmployee = new Employee();  
  15.             String firstName = "Narasimha";  
  16.             String lastName = "Reddy";  
  17.             String expected = "Narasimha Reddy";  
  18.             String actual;  
  19.             //Act  
  20.             actual = objEmployee.GetName(firstName, lastName);  
  21.             //Assert  
  22.             Assert.AreEqual(expected, actual);  
  23.         }  
  24.   
  25.     }  
  26. }  
Employee Class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace BusinessManager  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public string GetName(string firstName, string lastName)  
  11.         {  
  12.             return string.Concat(firstName," ", lastName);  
  13.         }  
  14.   
  15.   
  16.     }  
  17. }  
I hope this will help you to get an idea about Unit Testing.

 

Up Next
    Ebook Download
    View all
    Learn
    View all