DEV Community

Cover image for What is Unit Testing? Why Unit Testing is Important?
Ruhul Amin
Ruhul Amin

Posted on

What is Unit Testing? Why Unit Testing is Important?

What is Unit Testing?

Unit Testing is a type of software testing where individual units or components of software are tested. The purpose is to verify that each unit of the software code works as expected. Units are tested by developers during the development (coding phase) of an application. The unit isolates a section of test code and verifies its accuracy. A unit can be an individual function, method, method, module or object.

The main aim is to isolate each unit of the system to identify, analyze and fix the defects.

Coding is an important step in software development. So before doing this coding, the requirements are collected and analyzed, the architecture is designed, the platform, tools and programming language are decided. Then if the project gets bigger, coding is started by going through some more steps (like class and interaction design). Now an important thing when writing code is unit testing where different units are tested. Now what does the unit mean? A module can be considered as a single, interface can be considered as a unit, class can be considered as a unit, and function can also be considered as a single. I will now talk about writing test code with a single function. When we write big programs, we write each task into a separate function and one function will do only one task. If it is ever seen that a function is doing more than one thing, then it is necessary to break down the function and write smaller functions. So this function is the smallest unit.

Unit Testing Advantages

Unit Testing - Advantages:

  • Reduces Defects in the Newly developed features or reduces bugs when changing the existing functionality.

  • Unit Testing will reduce cost as defects are captured at an early stage.
    It improves design and allows better code refactoring.

  • Improves design and allows better refactoring of code.

  • Unit Tests, when integrated with build gives the quality of the build as well.

How to do unit test?

After writing the function, you have to write another function in a separate file for that function which will call that function through different input parameters. Then we have to compare the output to see if the output we are expecting and the output the function is giving are equal. Diameter, our unit became test. Different packages have been developed to facilitate unit testing in all programming languages ​​prevalent in the industry. Although the unit can be tested without using these packages.

Unit Testing LifeCyle:

Unit Testing LifeCyle

Unit Testing Techniques:

  • Black Box Testing - Using which the user interface, input and output are tested.

  • White Box Testing - used to test each one of those functions behaviour is tested.

  • Gray Box Testing - Used to execute tests, risks and assessment methods.

Top comments (4)

Collapse
 
sorasan profile image
Diego Fernando Mera Largo

So, I haven't "worked" with tests yet, and I was wondering, what are the differences between tdd and unit test and when I should use one or another? ヾ(•ω•`)o

Collapse
 
ruhulamin77 profile image
Ruhul Amin • Edited

I think it will be helpful for you...
baeldung.com/cs/unit-testing-vs-td....

Collapse
 
juandadev profile image
Juanda Martínez

This is the best short explanation I needed to know and remember this concept. Thanks a lot!

Collapse
 
ruhulamin77 profile image
Ruhul Amin

Thanks for your valuable opinion.