Unit Testing

Unit Testing

Before talking about Unit Testing, We need to understand levels of Testing, and we have already discussed about Levels of Testing in our last session. Just to give a summary, there are 4 different Levels of Testing

Different Levels of Testing

  1. Unit Testing
  2. Integration Testing
  3. System Testing
  4. UAT (User Acceptance Testing)

So, Let’s understand Unit Testing now.

What is Unit Testing

Unit Testing is a type of software testing where a unit or a component of a software is tested individually or independently.
Each and every unit of a software should be tested individually in Unit Testing.
Unit Testing is performed by Software Developers.
The Testing done by developers is called White Box Testing, that’s why Unit Testing is a White Box Testing.
White Box Testing is done to test the code of the Application, So Unit Testing is also done to test the code of the application.
Unit Testing is the very first level of testing in a Software Development Life Cycle (SDLC) or in Software Testing Life Cycle (STLC).

In Order to understand, Unit Testing in detail. Let’s see an Example.

Example of Unit Testing

Example 1

Let’s take an example of a Car. We have a components in a Car such as Wheels, Engine, Steering, Tyre, Accelerator, Breaks, Clutch etc. . All these components or units should be tested as a unit individually and then only they should be installed in the car.

Example 2

Let’s take an example of a Calculator. Let’s assume, there is a project to build a Calculator Application for Mobile. So, the developers will develop it step by step or you say, Module by Module. So, First they have to identify all the Modules for a Calculator, and let’s say that the different modules or Units of the Calculator are Addition, Subtraction, Multiplication and Division.

So, when these Units are developed, then the Developer will have to test these units independently. Let’s see, How a developer will do it. Let’s say the programming language that the Developer has used is Java.

public class Unit {
    public int add(int a, int b) {
        int c = a+b;
        return c;
    }
}
@Test
public class UnitTest {
    public boolean testAdd() {
        Unit obj = new Unit();
        int result = obj.add(2, 3);
        boolean c = Assert.assetEquals(result, 5);
        return c;
    }
}

So, We can see in the above code that the Developer has written the code for addition functionality in the Unit Class. Now, the Developer wants to test that the code for the Addition Functionality is working fine or not. So, Developer will write a Code for Testing the Addition Functionality in UnitTest Class. Developer will use Junit as a Unit Testing tool because Java is the Programming Language, and will make sure that the Code is working fine. @Test is the annotation in Junit for executing the test and Assertions are used for verification.

Same way, when the Developer will have to test the other units such as Subtraction, Multiplication, Division, then the developer will write code for the units in Unit class and then will write the Code for Testing in UnitTest Class.

Once the Unit Testing is completed, then only the Integration Testing will take place.

Importance of Unit Testing

Here are some reasons why unit testing is important:

  1. Early detection of defects: Unit Testing the very first level of Testing, So We can find defects at a very early stage of Testing, So Unit Testing helps in finding defects at an early stage.
  2. Cost effective: Unit testing helps in finding defects at an early stage in the development cycle when it is easier and less expensive to fix the defects. Because each unit is tested individually, So We will know, which unit has what defect and we can fix it easily, but when we find a defect at the time of System Testing, then it is difficult to find, which particular unit is causing the issue.
  3. Improved code quality: Unit Testing is a White Box Testing, which means In Unit Testing, Code of the Software is Tested, So Unit testing helps developers to write better code by testing the functionality of each unit. This, in turn, leads to better code quality, and reduced complexity.
  4. Faster development cycles: Because it is very easy to find defects, fix defects in Unit testing So, we can say that Unit Testing speeds up the development cycle by reducing the time required for debugging and troubleshooting.
  5. Better maintainability: Unit Testing helps in Maintaining the Code in a better way. Whenever developer write a new code for a new feature, then the developer can do the Unit Testing and maintain the code based on the Results of Unit Testing.
  6. Confidence in code changes: Because of all the above benefits of Unit Testing like, Early defect detection, Cost effectiveness, Better code quality, Faster developement, and Better maintainability, Developer gets a confidence that the Code will not break the existing functionality, and Developers can make changes more confidently

In summary, unit testing is important because it helps catch defects early, improves code quality, speeds up development cycles, enhances maintainability, and provides confidence in code changes.

Limitations of Unit Testing

We have seen the Importance of Unit Testing, But there are some limitations associated with Unit Testing, and here are those limitations.

  1. Coding Knowledge is required – Unit Testing is a type of White Box Testing, and White Box Testing is done on the Code of the Software Application but not on the Software Application, So you need to have coding Knowledge in order to perform Unit Testing.
  2. Only Code related issues can be found – Unit Testing is done on the Code of the Software Application, So we can only find Coding related issues.
  3. Configuration related issues can’t be found – Unit Testing is not done on the Application, So We can’t find the Configuration related issues, or we can say that we can’t find issues which can be found by the end user.
  4. Limited Scope – Unit Testing just tests 1 Unit of the Software Application, Not the complete Software Application or the 2 Integrated Units or more than 2 Integrated Units, So Unit Testing has very limited Scope in Testing.
  5. Time and Effort – Whenever we do any type of Testing, such as System Testing, Unit testing or any testing, We need to write Test Scenarios and Test Cases, but Writing Test Cases and Test Scenarios for Unit Testing is very difficult these days, when we have to follow agile process where we have very limited time for development and Testing.
  6. Performance Testing – Unit Testing does not help in performance Testing as we can check the Performance of a Software Application only when it is completely developed.

So, We can see that Unit Testing is important but there are some limitations of Unit Testing, which has to be overcome, and that’s why a Software is Tested at different levels, and we can overcome these limitations in Integration Testing, System testing, and UAT.

Summary

We have covered below Topics in this session

  1. Different Levels of Testing
  2. What is Unit Testing
  3. Example of Unit Testing
  4. Importance of Unit Testing
  5. Limitations of Unit Testing

Question

Explain Unit Testing with Example and what is the Importance of Unit Testing ?

Leave a Comment