Unit Testing

In unit testing, the complete application is broken into small independent units and tested without examining the relationship between modules. In simple terms it means that we do not test whether the modules are working successfully, we only test whether the smallest unit in the module is working.

In this article we cover the following topics

  1. What is Unit Testing
  2. Examples of Unit Testing
  3. Frameworks for Unit Testing
  4. Tests Excluded in Unit Testing
  5. Objectives of Unit Testing
  6. Features of Good Unit Test Cases
  7. How To Perform Unit Testing?
  8. Advantages of Unit Testing
  9. Disadvantages of Unit Testing
  10. Summary

What is Unit Testing?

Unit testing is one level of software testing where developers or white box testers test the individual units of code to see if these units are working correctly as per the design required.

Unit testing is not a new phenomenon. It has been going on from the time software programming began.

In procedural programming, units to be tested can mean individual programs, interfaces, functions, procedures, and so on. But, if you look at object-oriented programming, you will find that the smallest segment of code to be tested is a method belonging to a base class, abstract class or super class.What is Unit Testing

Developers use frameworks, stubs, drivers and fake objects to help in easy unit testing. Unit testing is very important as it ensures the code written by the developer is working.

Unit testing is the first level of testing. It is carried out before the next level called integration testing begins.

Examples of Unit Testing

Your project team is working on developing a hospital management system. One of the team members has been assigned the User Profile management module.

The User Profile management module allows the user to register their account on the website, view their profile, update their profile details, delete their account.

In this example, the developer will have to perform unit testing for each functionality separately. When the developer completes the registration functionality, they will have to perform unit testing for the User Registration functionalty.

Unit testing refers to the testing of the smallest unit. In this case, the developer will test if the user is able to register on the website successfully.

The registration functionality will have several test cases in itself, that will have to be tested. For example, there might be specific requirements related to

  • E-mail id validation: E-mail id entered should be a valid email id format like username@domain.tld
  • Phone number validation: Only 10 numerals can be entered
  • Password constraints: Password should be minimum 8 characters with one uppercase and one special character

All these test cases will be part of the unit testing of User Registration.

Once the developer completes the View Profile functionality they will perform unit testing on the View Profile functionality. Similarly, they will perform unit testing for Update Profile and Delete Profile functionality.

Frameworks for Unit Testing

Unit testing can be done manually or using software frameworks which can automate the task. Unit testing frameworks enables developers to write unit tests easily and quickly.

It assists the team to formalize the requirements, check the architecture, write, debug integrate and release codes, optimize and test the code. Third party open source tools used for unit testing are:

Framework Tool
Java framework JUnit
Python framework py.test
C++ framework UnitTest++ and Google C++
PHP framework PHPUnit
.NET framework NUnit

Tests Excluded in Unit Testing

During unit testing, the following tests will not be considered.

  • Database related testing
  • Network Communications
  • Test involving file systems
  • Tests that require changing the environment (eg. modification of config files)

Objectives of Unit Testing

  • Unit testing is done by developers to check if the code written by them meets the design, overall requirements and delivers what is expected from the application.
  • The aim of unit testing is to divide the application into the smallest separable parts called units and test those units independently to make sure it is working correctly.
  • Any functions and procedures that are used in the code, when given correct inputs deliver the expected outputs. It is also expected that the code will handle any failures gracefully.
  • Unit testing is a written contract that ensures the code meets the expected standard.

Features of Good Unit Test Cases

  • The unit test must be small, simple and isolated.
  • The unit test code must be written in order to clarify a single unit of code.
  • Each unit test should be named clearly.
  • Unit test code should not be used for testing integrations.
  • Reusable unit tests that run quickly must be written.

How To Perform Unit Testing? – Steps

  1. Prepare a test Plan for unit testing.
  2. Create unit test cases and test data.
  3. Write scripts to run the test cases.
  4. Run the tests.
  5. Fix the defects and retest the code again.
  6. Refactoring the code.
  7. Repeat unit testing till the independent units are defect free.

Advantages of Unit Testing

  • The main benefit of unit testing is that bugs in the code can be identified early. We get quick feedback. So, there is no chance of an impact on other areas of the code.
  • Unit testing helps to maintain and change the code without any difficulty because the codes are independent in this stage. Therefore, making a change in code does not create an impact on other modules/codes.
  • As individual units are already verified during unit testing, integration testing is simplified to a great extent and takes comparitively lesser time and effort.
  • Unit testing helps in improving the efficiency of code changes and maintenance.
  • Code coverage can be measured easily.
  • Unit testing helps to reduce cost of bug fixing because bugs are found in a early stage in the development life cycle.
  • Unit testing makes debugging easier as the developer only needs to debug the latest code if a test fails.

Disadvantages of Unit Testing

  • Unit testing cannot guarantee absence of errors.
  • Unit testing can be used only to test the functionality of independent units.
  • Strict Discipline and consistency is needed
  • Unit testing cannot identify performance errors and integration errors.
  • Unit testing requires a lot of time.
  • Version control system may be required.
  • There are limitations to the number of scenarios, parameters and data that can be used to test the source code.


Unit testing is the initial and most important level of testing and must not be neglected. Identifying the bug in the early stages through unit testing can help the organization save on costs and time in the long run.