Test Driven Development - What, Why and Practicing (using C#, ASP.NET, MVC5 and NUnit)


In this article, I'm trying to summarize our 2-hours session (Webinar). Its hard to capture each and every thing in this short/concise article. I will try to cover all the things what we have discussed.

  • Abstract

  • Introduction

  • Pre-requisite

  • Lets start

  • What is TDD?

  • Why is TDD?

    • As a developer why shoud I use TDD?


    • Practicing TDD

    • Lets start practicing with simple TDD Katas


    • Working With Code

    • What to next?

    • Conclusion



    • Abstract


      This article is showing glimpse of my recent webinar of dotnetspider. More info: Webinar - Test Driven Application development.
      Where, we discussed all about TDD:

      - What
      - Why
      - Practicing TDD

      Introduction


      In this article, I'm trying to summarize our 2-hours session (Webinar). Its hard to capture each and every thing in this short/concise article.
      I will try to cover all the things what we have discussed.

      Pre-requisite


      Just to read this article doesn't mean that you've got the each and everything about the concept TDD. But, you need to start practicing TDD on daily basis.
      For the same you need following:

      - Visual Studio 2010 or later
      - Basic knowledge of C#
      - Basic knowledge of ASP.NET (if want to practice with this)
      - Basic knowledge of ASP.NET MVC4/MVC5 (if want to practice with this)
      - Basic knowledge of GIT
      - Basic knowledge of Nuget Packages
      - Basic knowlegde of NUnit

      Lets start


      Hope, you've reached here and you've all above things with you. So, lets get start.

      What is TDD?


      TDD or Test Driven Development is an evolutionary approach.

      This is a way for a developer to think through actual requirements or design before to start writing actual functional code.

      In other aspect we can say that TDD is a technique to write a clean code as per actual requirements and design.

      In general practices developer write a test before actual function on the failure of test, write function, checks test, refactoring and continue tests.

      Why is TDD?


      As we discussed above, TDD is a technique, which gives a way to write a clean code. So, we can say that TDD is important to cut off extra development
      efforts and cost due to:

      - bugs like overflow
      - not as per requirements
      - redundant code or duplicate code

      TDD provides facility to test code within code.

      I personaly believe in TDD and always try to implement in my real time projects.

      As a developer why should I use TDD?


      TDD is might help a developr to:

      - understand the functionality/code, what he/she is going to write.
      - forces developer to make a practice to write tests
      - give a speedy development
      - provides a way to better implementation

      Practicing TDD


      So, going forward in TDD era, we need to understand how to and where to start practicing of TDD. In my view the best approach to learn TDD is using TDD Katas.
      Discussing TDD Katas is out of our scope to understad the same I would recommend to visit my Tech net article:
      - Learning Test Driven Development with TDD Katas.

      Lets start practicing with simple TDD Katas


      As we have discussed above, the best way to start learning TDD is TDD-Katas. So, lets start prepare a simple/basic kata and start practice TDD using TDD-Katas:

      Simple TDD-Kata


      Lets start writing few of rules for our Kata, first of all name it, I would like to call this kata String Sum Kata.

      Problem


      Here are complete things we need to perform during practice:
      - Write a simple String Sum utility with a function string Sum(string num1, string num2), which can accept only natural numbers and will return their sum.
      Replace entered number with 0 (zero) if entered number is not a natural number.

      Solution


      Lets divide our solutions in following steps:
      - Stat with a simplest test case with an empty string
      - Create a simple method string Sum(string num1, string num2)
      - Write a test to pass small numbers and refactor, if test passed
      - try to write more code and refactor

      Working with code


      Lets work with some real code, so, open your Visual Studio add two classes StringSumKata.cs and TestStringSumkata.cs.

      Open your TestStringSumKata.cs and write below mentioned code:


      [TestCase("",null,0)]
      [Test]
      public void AddReturnSum(string num1, string num2, string expectedResult)
      {
      //call method here
      }


      Initially, we added a test without its actually functionality. Now, go back and read our requirements to write String sum utility.

      Write code to meet your initial functionality in StringSumKata.cs


      public static string Sum(string num1, string num2)
      {
      var realNum1 = string.IsNullOrEmpty(num1) ? "0" : num1;
      var realNum2 = string.IsNullOrEmpty(num2) ? "0" : num2;

      return Convert.ToString(int.Parse(realNum1) + int.Parse(realNum2));
      }


      Now, correct the test:


      [TestCase("",null,"0")]
      [Test]
      public void AddReturnSum(string num1, string num2, string expectedResult)
      {
      var result = StringSumKata.Sum(num1, num2);

      Assert.That(expectedResult, Is.EqualTo(result));
      }


      Go and refactor, a bit:


      public static string Sum(string num1, string num2)
      {
      var realNum1 = GetZeroWhenNullOrEmpty(num1);
      var realNum2 = GetZeroWhenNullOrEmpty(num2);

      return Convert.ToString(Add(realNum1, realNum2));
      }

      private static string GetZeroWhenNullOrEmpty(string num1)
      {
      return string.IsNullOrEmpty(num1) ? "0" : num1;
      }

      private static int Add(string realNum1, string realNum2)
      {
      return int.Parse(realNum1) + int.Parse(realNum2);
      }


      Go, and run test again, if passed, then get back to your requirements and repeat all above step(s) untill you complete all the requirements.

      What to next?


      We have seen a lot about TDD. Now, go and grab one of the following and start working/learning TDD:

      - Test Driven Development:GIT
      - Test Driven Development:Nuget Package

      Following is the Video Tutorial on TDD What, Why and Practicing



      Conclusion


      In this article we discussed:
      - TDD is not a unit testing
      - TDD is writing and testing code at the same time.
      - With TDD we make sure that code is tested and refactored.
      - The best way to practice TDD is using TDD-Katas


  • Article by Gaurav Aroraa
    Gaurav is a Microsoft Technology Specialist professional. He has awarded lifetime membership from Computer Society of India (CSI). He has more than 13yrs of experience in the industry. Currently, he is working in the capacity of Solution Architect with an MNC. He is serving to the various communities since 1999.

    Follow Gaurav Aroraa or read 149 articles authored by Gaurav Aroraa

    Comments

    Author: Sridhar Thota05 Jul 2015 Member Level: Gold   Points : 2

    Hi Gaurav Sir.

    Thanks for writing article on the session delivered by you on Test Driven Development.

    The members who didn't attended the webinar on 28th can get benefit from this article.

    Thanks for uploading video as well.

    Regards

    Sridhar Thota.

    Author: Gaurav Aroraa05 Jul 2015 Member Level: Gold   Points : 0

    Thanks Sridhar for your support.
    Yes, I created video so others can get benefited.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: