Unit Testing in Software Engineering

Unit Testing

  • In software engineering, unit testing is a level of software testing in which individual units/components are tested. 
  • A unit is a smallest testable part/module of any software application. In procedural programming, a unit is an individual program, function, procedure. In object-oriented programming, a unit may be a method.
  • Unit testing of the software product is done during the software development stage saves time and money in the end. 
  • If unit testing is skipped by the software developers then there is higher chances of defects during the integration, system, acceptance and also beta testing.
  • The goal of unit testing is to isolate each part of source code and verifies that each part works properly as designed and to check that source code meets the requirements and gives the expected output. 
  • The main advantage of unit testing is that it identify the problem earlier in the application and if any issue occurs then it can be fix before integrate the units.
  • In SDLC, STLC, V Model, unit testing is the first level of software testing done before integration testing.

  • Unit testing is a white box testing which is performed by the software developers and in rare cases, this testing may be done by independent software testers and quality assurance engineers also do unit testing.

Unit Testing

Objective of unit testing :

  1. Unit testing separate a section of code.
  2. To verify that source code performs as designed in an expected manner.
  3. This testing is used to test every function and procedure individually.
  4. To fix defects early in software development stage.
  5. Software developers performs this testing to save time and cost both.
  6. Unit testing helps the developers to understand the code base and enable them to make changes quickly.
  7. To help for code reuse.

Workflow of Unit Testing:

The workflow of Unit Testing is given below : 

  1. Design Test Cases
  2. Review/Rework
  3. Baseline
  4. Execute Test Cases

Types of Unit Testing:

There are 2 type of Unit Testing:

  • Manual
  • Automated

A manual approach may employ a step-by-step instructional document. Unit testing is commonly automated but may be performed manual approach. A coder uses a unit-test framework to design automated test cases.

Unit Testing Tools:

  1. Jtest
  2. JUnit & EMMA : Java framework 
  3. NUnit : .NET framework
  4. PHPUnit : PHP framework
  5. py.test : Python framework
  6. UnitTest++ & Google C++ : C++ framework
  7. test-unit : Ruby framework

Advantages of Unit Testing:

  • Unit testing allows the programmer to improve the code and make sure that the components works properly in the expected manner.
  • Unit testing enables to verify units of an application without waiting for others to be completed due to the modular nature of this testing.
  • This testing reduces bugs in the newly developed features or when altering the existing working of an application.
  • It helps to maintain the code. 
  • This testing is used to reducing the cost of defect fixes because defects are found early in the software development stage. 
  • Fixing defects in unit testing can fix many other defects arising in later development.
  • Due to decreased defect count overall development time can be saved to complete a project.

Disadvantages of Unit Testing:

  • Unit testing can not be expected to find all defects/errors present in a program/code. 
  • Unit testing is not able to find integration level errors or system level errors because it only focuses on components of the code/program.
  • This testing can be time-consuming.