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
So, Let’s understand Unit Testing now.
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.
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.
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.
Here are some reasons why unit testing is important:
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.
We have seen the Importance of Unit Testing, But there are some limitations associated with Unit Testing, and here are those limitations.
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.
We have covered below Topics in this session
Explain Unit Testing with Example and what is the Importance of Unit Testing ?
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…