Test Driven Development (TDD) - Part Two

I am here to continue the thread related to Test Driven Development. We will be going through the options available in Visual Studio to work with TDD, followed by examples to demonstrate how to create unit test cases.

If you are new to TDD and haven’t gone through the first part of the series, I would recommend to go for that first in order to understand the basics of it. Below is the link.

In the previous article, we mentioned that unit tests are coded to test specific aspects of a functionality. Now, let’s talk a bit about some other terms used here before jumping to “how to do TDD”.

Mocking

Mocking is a method to create a dummy object of any concrete object. At times, when we test any particular method and it has a set of dependencies, we create a mock or dummy object to mimic the real object or dependency.

Code Coverage

Code coverage is the term used to present the percentage of code blocks covered as part of unit test cases. Generally having code coverage more than 80% is considered as good coverage however in some cases even 70% coverage is considered as good especially when there are lot of UI components involved.

Visual Studio support for Test Driven Development

Visual Studio provides great tools and support for Test Driven Development. Below are some of the options available in Visual Studio for the same.

  • Test Explorer

    Test Explorer is a one stop shop for executing unit test cases and analyzing the failures. We can execute the test cases either one by one or as a group.


  • Code Coverage

There are two ways available to extract the code coverages in Visual Studio as following,

  • Test Menu -> Analyze Code Coverage -> Selected Tests/ All Tests
  • Right click on Test from Test Explorer / or To any group –> Analyze Code Coverage for Selected Test


We can enable the “Show Code Coverage Coloring” option in the “Code Coverage Results” dialog window. Given are the indicators along with their color coding to help developers understand the code coverage in respective code blocks.

  • covered ( light blue)
  • partially covered ( light orange)
  • not covered (light red)

For example, the below screenshot represents the covered code blocks.


We can also enable the option to execute unit tests after each successful build in Visual Studio as following.

Test Menu–> Test Settings and select “Run Tests After Build”

Test Driven Development using Visual Studio

There are several ways to work with TDD in Visual Studio, as following.

  • Creating both unit test cases and logic manually
    This option involves including Microsoft.VisualStudio.TestTools.UnitTesting namespace and decorating the class as [TestClass] and method as [TestMethod].

Let’s take example of two methods and try to create unit tests for those.

  1. public interface IAccount  
  2.     {  
  3.         double CurrentBallance { get; }
  4.         double Deposit(double amount);  
  5.         double Withdraw(double amount);  
  6.     }  
  7.   
  8. public class Account : IAccount  
  9.     {  
  10.         double _balance;  
  11.         
  12.         public double CurrentBallance { get { return _balance; } }
  13.           
  14.         public double Deposit(double amount)  
  15.         {  
  16.          if (amount > 0)
             {
  17.             _balance += amount;  
  18.             return _balance;  
  19.          }  
  20.         else
             {
                throw new ArgumentException("Invalid Deposit Amount!");
             }
  21.         }
  22.         public Double Withdraw(double amount)  
  23.         {  
  24.             if (amount > 0 && _balance >= amount)  
  25.             {  
  26.                 _balance -= amount;  
  27.             }  
  28.             else  
  29.             {  
  30.                 throw new ArgumentException("No balance to Withdraw!");  
  31.             }  
  32.   
  33.             return _balance;  
  34.          }
Below are the unit tests created manually for the above methods.
  1. [TestClass()]  
  2.     public class AccountTests  
  3.     {  
  4.          [TestMethod()]
             public void CurrentBallanceTestPositiveCase()
             {
                IAccount account = new Account();
                account.Deposit(100);
                double result = account.CurrentBallance;
                Assert.AreEqual(100, result);
             }
     
  5.         [TestMethod()]  
  6.         public void DepositTestPositiveCase()  
  7.         {  
  8.             IAccount account = new Account();  
  9.             double result = account.Deposit(100);  
  10.             Assert.AreEqual(100, result);  
  11.         }  

  12.          
             [TestMethod()]
             [ExpectedException(typeof(ArgumentException))]
             public void DepositTestNegativeAmount()
             {
                IAccount account = new Account();
                double result = account.Deposit(-100);
             }
            
  13.         [TestMethod()]  
  14.         public void WithdrawTestPositiveCase()  
  15.         {  
  16.             IAccount account = new Account();  
  17.             account.Deposit(500);  
  18.             double result = account.Withdraw(400);  
  19.             Assert.AreEqual(100, result);  
  20.         }  
  21.   
  22.         [TestMethod()]  
  23.         [ExpectedException(typeof(ArgumentException))]  
  24.         public void WithdrawTestNegativeWithdrawAmount()  
  25.         {  
  26.             IAccount account = new Account();  
  27.             account.Deposit(200);  
  28.             double result = account.Withdraw(-300);             
  29.         }  
  30.   
  31.         [TestMethod()]  
  32.         [ExpectedException(typeof(ArgumentException))]  
  33.         public void WithdrawTestWithMoreWithdrawAmountThanDeposit()  
  34.         {  
  35.             IAccount account = new Account();  
  36.             account.Deposit(200);  
  37.             double result = account.Withdraw(300);             
  38.         }  
  39.     }  
  • Creating test case prototype by tools and logic manually

This option has been introduced in Visual Studio 2015 and we will be covering this option in the next part in detail.

  • Creating both unit test cases and logic by tools
This option has been introduced in Visual Studio 2015 and we will be covering this option in the next part in detail.

Conclusion

In this article, we have gone through the options available in Visual Studio to work with Test Driven Development. In the end, we have also taken examples to understand about writing unit test cases.

You can also download the attached demo project (TDD_Manual.zip) to go through the full source code referred in the article.

I will cover the remaining aspects of TDD in subsequent parts.

Hope you have liked the article. Look forward for your comments/suggestions.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now