Unit testing involves the testing of each unit or an individual component of the software application. It is the first level of functional testing. The aim behind unit testing is to validate unit components with its performance.

A unit is a single testable part of a software system and tested during the development phase of the application software.

The purpose of unit testing is to test the correctness of isolated code. A unit component is an individual function or code of the application. White box testing approach used for unit testing and usually done by the developers.

Whenever the application is ready and given to the Test engineer, he/she will start checking every component of the module or module of the application independently or one by one, and this process is known as Unit testing or components testing.

Why Unit Testing?

In a testing level hierarchy, unit testing is the first level of testing done before integration and other remaining levels of the testing. It uses modules for the testing process which reduces the dependency of waiting for Unit testing frameworks, stubs, drivers and mock objects are used for assistance in unit testing.

Unit Testing

Generally, the software goes under four level of testing: Unit Testing, Integration Testing, System Testing, and Acceptance Testing but sometimes due to time consumption software testers does minimal unit testing but skipping of unit testing may lead to higher defects during Integration Testing, System Testing, and Acceptance Testing or even during Beta Testing which takes place after the completion of software application.

Some crucial reasons are listed below:

  • Unit testing helps tester and developers to understand the base of code that makes them able to change defect causing code quickly.
  • Unit testing helps in the documentation.
  • Unit testing fixes defects very early in the development phase that's why there is a possibility to occur a smaller number of defects in upcoming testing levels.
  • It helps with code reusability by migrating code and test cases.

Example of Unit testing

Let us see one sample example for a better understanding of the concept of unit testing:

Unit Testing

For the amount transfer, requirements are as follows:

1.Amount transfer
1.1From account number (FAN)→ Text Box
1.1.1FAN→ accept only 4 digit
1.2To account no (TAN)→ Text Box
1.2.1TAN→ Accept only 4 digit
1.3Amount→ Text Box
1.3.1Amount → Accept maximum 4 digit
1.4Transfer→ Button
1.4.1Transfer → Enabled
1.5Cancel→ Button
1.5.1Cancel→ Enabled

Below are the application access details, which is given by the customer

  • URL→ login Page
  • Username/password/OK → home page
  • To reach Amount transfer module follow the below

Loans → sales → Amount transfer

While performing unit testing, we should follow some rules, which are as follows:

  • To start unit testing, at least we should have one module.
  • Test for positive values
  • Test for negative values
  • No over testing
  • No assumption required

When we feel that the maximum test coverage is achieved, we will stop the testing.

Now, we will start performing the unit testing on the different components such as

  • From account number(FAN)
  • To account number(TAN)
  • Amount
  • Transfer
  • Cancel
Unit Testing

For the FAN components

Values

Description

1234

accept

4311

Error message→ account valid or not

blank

Error message→ enter some values

5 digit/ 3 digit

Error message→ accept only 4 digit

Alphanumeric

Error message → accept only digit

Blocked account no

Error message

Copy and paste the value

Error message→ type the value

Same as FAN and TAN

Error message

For the TAN component

  • Provide the values just like we did in From account number (FAN) components

For Amount component

  • Provide the values just like we did in FAN and TAN components.

For Transfer component

  • Enter valid FAN value
  • Enter valid TAN value
  • Enter the correct value of Amount
  • Click on the Transfer button→ amount transfer successfully( confirmation message)

For Cancel Component

  • Enter the values of FAN, TAN, and amount.
  • Click on the Cancel button → all data should be cleared.

Unit Testing Tools

We have various types of unit testing tools available in the market, which are as follows:

  • NUnit
  • JUnit
  • PHPunit
  • Parasoft Jtest
  • EMMA

 

Unit Testing Techniques:

Unit testing uses all white box testing techniques as it uses the code of software application:

  • Data flow Testing
  • Control Flow Testing
  • Branch Coverage Testing
  • Statement Coverage Testing
  • Decision Coverage Testing

How to achieve the best result via Unit testing?

Unit testing can give best results without getting confused and increase complexity by following the steps listed below:

  • Test cases must be independent because if there is any change or enhancement in requirement, the test cases will not be affected.
  • Naming conventions for unit test cases must be clear and consistent.
  • During unit testing, the identified bugs must be fixed before jump on next phase of the SDLC.
  • Only one code should be tested at one time.
  • Adopt test cases with the writing of the code, if not doing so, the number of execution paths will be increased.
  • If there are changes in the code of any module, ensure the corresponding unit test is available or not for that module.

Advantages and disadvantages of unit testing

The pros and cons of unit testing are as follows:

Advantages

  • Unit testing uses module approach due to that any part can be tested without waiting for completion of another parts testing.
  • The developing team focuses on the provided functionality of the unit and how functionality should look in unit test suits to understand the unit API.
  • Unit testing allows the developer to refactor code after a number of days and ensure the module still working without any defect.

Disadvantages

  • It cannot identify integration or broad level error as it works on units of the code.
  • In the unit testing, evaluation of all execution paths is not possible, so unit testing is not able to catch each and every error in a program.
  • It is best suitable for conjunction with other testing activities.