Fundamentals of Unit Testing: Getting Started With Unit Testing

Here I will write a series of articles about unit testing. Truth to be told, I am neither good nor a master of testing. So, I decided, let's learn unit testing and along with that, share my experience and learning with community. I think it would be better if I could provide a title something “with testing with me or let's learn unit testing” or very similar with this. Anyway, our motto is to learn unit testing, the title is not much important. Haha.

Ok, and a point to mention is that this article is purely for beginners, those who want to get their hand dirty with unit testing. I hope we all have the concept of test driven development methodology where testing performs a great role in development.

Fine, we are familiar with that more or less. Now, the question is, what is unit testing? What does unit mean? Why apply unit testing methodology when we can test the entire functionality after the implementation?

We will explain all, one by one. First of all, what is unit?

Unit at a glance

The unit indicates a unit of code or we can say a few lines of code. The unit of code may point to a method or may point to a function or may point to class or even one module. So, there is no perfect definition or amount limitation of a unit. It might be anything at the end of the day, it's a few line of code that performs certain functionality.

Advantages of testing code as a unit

Let's use a practical example to understand. Assume we want to build a strong wall and it will be nearly unbreakable. Now if we test the quality of the break individually or one break of the same category then is it necessary to test the entire wall ? Or the notion is something like, we want to ensure that Rice has boiled properly and to prove that, we do not test each and every grain and it should not be.

That's why a unit test is very important. If we test each and individual function then each and every class then we might avoid difficulty after integration of all modules.

Problems with manual testing

Manual testing is very good and efficient to find most of the bugs (after all its human interaction) but there are certain limitations of manual testing. For example,

We need to develop a class library (let's think of class library development in C# .NET with Visual Studio, since we love Microsoft) and as we know, a class library does not have a user interface, it's nothing but a set of classes and functions. Then what is the solution? How will we provide input to those functions and how will we check whether or not it's working?

And the unit testing technique is there to rescue us.

Fine, I understood; how do I do unit testing?

This is the key discussion throughout this series. How to do unit testing. There are many options for you. Let's explain them one by one.

  • Third-party Unit Testing framework support

    Previously the earlier versions of Visual Studio had less tooling support for third-party Unit Testing frameworks. The new Unit Test Explorer can also be configured to run Unit Tests from other frameworks such as xUnit.NET, MbUnit and NUnit. All you need to do is to install the appropriate Unit Test adapter. Please see the available Unit Test adapters for Visual Studio 12 or later.
     
  • Use Unit Testing template of Visual Studio

    This is another option to perform unit testing. Visual Studio has provided an integrated template to perform unit testing in the same environment. And the good news is, we will do unit testing in the same template. I am interested in tlking various unit testing frameworks on another day.

Which Visual Studio to use

We are free to use any version of Visual Studio (greater then 2008) but there were problems earlier when it was tough to migrate a unit test project from Visual Studio 2008 to Visual Studio 2010. It's something like this (VS2010 upgrade with UnitTestFramework). So, I will strongly recommend you to use Visual Studio 2010 or greater. In this series, we will use Visual Studio 2012. And Microsoft itself insists that there is no issue to migrate a project from Visual Studio 2010 to 2012. This is partly due to the fact that most of the unit test components are the same between those two versions of Visual Studio. The library is still microsoft.VisualStudio.QualityTools.unitTestFramework.dll in its version 10.0.0.0, that is still based on .NET runtime v2.0.50727.

Basic guideline of test driven development

This is the last topic of this introductory article, here we will discuss three important phases of TDD (Test Driven Development).

Step 1: Write code or functionality implementation

We all know that this must be done as the very fast step. We will understand business logic and will implement it in code.

Step 2: Write test case

Here we will write all the test cases necessary to do the business logic. For example, if we write a function to send mail, then we will define all the test cases to check which condition the mail will forward and in which condition it will get fail.

Step 3: Refine/refractor your code and get extra benefit

The process of improving your source code after the initial implementation is called “Refactoring”. The source code structure is modified internally without any modifications to the external behavior (very important!!). A source code that just “works” is now transformed into a source code that works in an optimal way. Most of the time, the resulting source code is executing with better performance, using less memory and/or with a better software design.

The refactoring consists of the following steps (non-exhaustive list):

  1. Detect and eliminate all code duplication
  2. Limit complexity and the number of classes
  3. Simplify and optimize method algorithms
  4. Relocate, rename and harmonize methods
  5. Improve code readability
  6. Remove not used code (also called “dead code”)
  7. Add comments to complex code sections

Conclusion

This is the introductory article to unit testing. Please don't be upset, from the next article onwards we will deal with code. We will see examples of unit testing with live examples. Thanks for reading. Happy .NEting.

Up Next
    Ebook Download
    View all
    Learn
    View all