Learn About Unit Testing

As per Wikipedia, the definition of Unit Testing is -
 
"In computer programming, 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."
 
In simple words, we can say that unit testing is a function that tests a unit of work.
 
For example, a divide function is a unit of work.
 
Learn About Unit Testing
  1. namespace Calculators  
  2. {  
  3.     public class Divides  
  4.     {  
  5.         public static int divide(int numerator ,int denominator)  
  6.         {  
  7.             int result = numerator / denominator;  
  8.             return result;  
  9.         }  
  10.     }  
  11. }  
 

Popular Unit Testing frameworks 

  • MSTEST
  • Nunit
  • MbUNit
  • net
MSTest is by default integrated with Visual Studio.
 
There are three steps to create a UNIT Test
  1. Add a unit test project by your solution
  2. Decorate the class the contains test methods with TestClass attribute
  3. Decorate the test method with TestMathod attribute
That function which uses for testing we decorate with TestMethod attribute and Test class is decorated with TestClass attribute.
 
Step 1 - How to add Unit Test project in project solution?
  1. Right-click on the solution on Add New Project.
  2. Under the Visual C# click on Test option
  3. Seen in the screenshot I have selected unit test project (.net Framework)

    Learn About Unit Testing
After the add Unit test project in the solution. Create a test class which contains all test methods, class decorate with TestClass Attribute and methods decorate with TestMethod Attribute.
 
Add reference of the calculators class library project into the Calculator.Test project see below screenshot,
 
Learn About Unit Testing
 
Test case defined in three sections,
  • Arrange: initializes objects and set the value of the data that is passed to the method being tested
  • Act: invokes the method being tested
  • Asset: verifies that the method being tested behaves as expected
Learn About Unit Testing
  1. namespace Calculator.Test  
  2. {  
  3.     [TestClass]  
  4.     public class CalculatorTest  
  5.     {  
  6.         [TestMethod]  
  7.         public void Test_Divide()  
  8.         {  
  9.             // Arrange   
  10.             int expected = 10;  
  11.             int numerator = 100;  
  12.             int denominator = 10;  
  13.             // Act  
  14.           int actual=  Calculators.Divides.divide(numerator, denominator);  
  15.             // Asset  
  16.             Assert.AreEqual(expected, actual);  
  17.         }  
  18. }  
  19. }  
Assert class has many static method currently I have used AreEqual() this is use for check expected value is equal to actual value.
 
Naming Conventions is an important part for the unit test case, so I have changes name above test method name as per Roy Osherove’s naming strategy.
 
[UnitOfWork_StateUndertTest_ExpectedBehavior]
 
We got three method in the test class,
  • Divide positive number (Divide_PositiveNumbers_ReturnsPositiveQuotient)
  • Divide positive numerator with denominator (Divide_PositiveNumeratorAndNegativeDenominator_ReturnsNegativeQuotient)
  • Divide negative number (Divide_NegativeNumber_ReturnsPositiveQuotient)
Learn About Unit Testing
  1. namespace Calculator.Test  
  2. {  
  3.     [TestClass]  
  4.     public class CalculatorTest  
  5.     {  
  6.         [TestMethod]  
  7.         public void Divide_PositiveNumbers_ReturnsPositiveQuotient()  
  8.         {  
  9.             // Arrange   
  10.             int expected = 10;  
  11.             int numerator = 100;  
  12.             int denominator = 10;  
  13.             // Act  
  14.           int actual=  Calculators.Divides.divide(numerator, denominator);  
  15.             // Asset  
  16.             Assert.AreEqual(expected, actual);  
  17.         }  
  18.         [TestMethod]  
  19.         public void Divide_PositiveNumeratorAndNegativeDenominator_ReturnsNegativeQuotient()  
  20.         {  
  21.             // Arrange   
  22.             int expected =-10;  
  23.             int numerator = 100;  
  24.             int denominator = -10;  
  25.             // Act  
  26.             int actual = Calculators.Divides.divide(numerator, denominator);  
  27.             // Asset  
  28.             Assert.AreEqual(expected, actual);  
  29.         }  
  30.         [TestMethod]  
  31.         public void Divide_NegativeNumber_ReturnsPositiveQuotient()  
  32.         {  
  33.             // Arrange   
  34.             int expected = 10;  
  35.             int numerator = -100;  
  36.             int denominator = -10;  
  37.             // Act  
  38.             int actual = Calculators.Divides.divide(numerator, denominator);  
  39.             // Asset  
  40.             Assert.AreEqual(expected, actual);  
  41.         }  
  42.   
  43.         [TestMethod]  
  44.         [ExpectedException(typeof (DivideByZeroException))]  
  45.         public void Divide_DenominatorIsZero_ThrowDivideByZeroException()  
  46.         {  
  47.             // Arrange   
  48.           
  49.             int numerator = 100;  
  50.             int denominator = 0;  
  51.             // Act  
  52.             try  
  53.             {  
  54.                              
  55.           Calculators.Divides.divide(numerator, denominator);  
  56.             }catch(Exception ex)  
  57.             {  
  58.                 Assert.AreEqual("denominator is zero", ex.Message);  
  59.                 throw;  
  60.             }  
  61.   
  62.         }  
  63.     }  
  64. }  

Run the Unit Test Case

 
We can run the unit test in several ways,
  1. Run all test
  2. Run single test
  3. Run all test for the only selected test class
See below in the explorer window,
 
Right-click on the selected test class and select Run selected unit tests option as the same way we run a single unit test select particular Test Method to go to the same option which using in the above line.
 
Learn About Unit Testing
 

Grouping Unit Tests

 
Grouping Unit test in test Explorer
  • OutCome
  • Duration
  • Class
  • Project
  • Traits

Code Coverage in the Unit Test

 
Definition as per Wikipedia,
 
Test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high test coverage, measured as a percentage, has had more of its source code executed during testing, which suggests it has a lower chance of containing undetected software bugs compared to a program with low test coverage.
 
URL: 
 
In the simple term, Code coverage is finding out how much of our application code is being tested by automated unit tests
 
Now, I am using AxoCover For check Code coverage scope. The Code coverage scope is three-level
  1. Assembly level
  2. Class level
  3. Method level
Below screenshot display the report of coverages. Code coverage report show percentage of the code coverage at the assembly level, class and method level.
 
Having good code coverage does not mean the code is being tested well.
 
Learn About Unit Testing 
 

Testing Exceptions

 
Testing exceptions with showing specific messages. Check in the below code screenshot
 
The method name is decorate with [ExpectedException(typeof (DivideByZeroException))] attribute
 
In the code snippet, I have use try-catch block for specific message Asserting.
 
If we do not require to check any specific exception message then we can remove a try-catch block.
 
Learn About Unit Testing 
  1. [TestMethod]  
  2. [ExpectedException(typeof (DivideByZeroException))]  
  3. public void Divide_DenominatorIsZero_ThrowDivideByZeroException()  
  4. {  
  5.     // Arrange   
  6.   
  7.     int numerator = 100;  
  8.     int denominator = 0;  
  9.     // Act  
  10.     try  
  11.     {  
  12.                      
  13.   Calculators.Divides.divide(numerator, denominator);  
  14.     }catch(Exception ex)  
  15.     {  
  16.         Assert.AreEqual("denominator is zero", ex.Message);  
  17.         throw;  
  18.     }  

Assert

 
An Assertion is using to verifying the expected value to actual value.
 
Mstest provides three different class for the assertion 
  1. Assert
  2. collectionAssert
  3. StringAssert
In this article, I have used reference of Wikipedia and kudvenkat unit testing lecture series.