Unit Testing

What is Unit Testing

The testing of the smallest testable units of a software product is called Unit Testing. The smallest unit can be an individual method or a function or a program or it can be a logical group of programs which can be separately tested.

It is the type of testing which is performed at the lowest level entity or unit in the software product framework. Unit testing is also referred to as component testing or module testing.

Unit Testing

 

Why is unit testing required

Unit testing is done with the objective of finding failures from the code in the early stage of the software life cycle. It helps in identifying and fixing of defects from the beginning of code implementation. The identified defects are not passed to the next phase or iteration thereby improving the product quality and reducing the risks of failure.

How is unit testing performed

Unit testing mostly is done as per the White box testing methodology. In white box testing, the flow of code is evaluated and tested. Test cases are implemented in the code base itself using available testing frameworks of the programming language in which project is being developed. For instance, the unit test cases for Java are developed in Java itself using JUnit or cucumber testing frameworks.

Unit testing is typically done by the developers in their own development environment. To perform unit testing developer needs the source code of the module to be tested, requirement document or user stories and functional or technical specifications of the component under test to cross verify the test results.

During unit testing, the component under test is isolated from the rest of the system and is tested independently. To assist with the test data and functional flow for the purpose of testing, at times mocks are required. Mocks are the code units written to recreate the input and output behavior of the rest of the system.  External system outputs, services, and database flow required for testing can be mimicked with the help of mocks.

Unit Testing

What is the process of unit testing

Unit test cases are identified.  After identification, test cases are implemented in a unit testing framework. Test execution is done and bugs are found. Bugs are fixed by the developer and are re-verified.

Unit Testing

Examples of unit testing

Let’s take an example of a simple arithmetic calculator software system to understand unit testing. This Arithmetic calculator can take two numbers as input and prints one number as output. It consists of four methods addition, subtraction, multiplication and division as shown below:

Program calculator {

  Read number i
  Read character ae
  Validate if ae is valid arithmetic expression
  Read number j
  
  Declare number r
  
  If (ae = '+') call addition(i,j)
  If (ae = '-') call substraction (i,j)
  If (ae = '*') call multiplication (i,j)
  If (ae = '/') call division (i,j)
  
  Print r
  
  addition(num a,num b){
    number res
    res = a + b
    return res
  }
  
  substraction(num a,num b){
    number res
    res = a - b
    return res
  }
  
  division num a,num b){
    number res
    res = a / b
    return res
  }
  
  multiplication(num a,num b){
    number res
    res = a * b
    return res
  }
 }

The unit test will set the value of the input numbers and arithmetic expression. It will call the methods of the program and verify the output for its correctness. Sample unit test cases to test the calculator program is given below:

 

Program CalculatorUnitTest{
 
  Test()
  verifyinputs(){
    i = 'c'
    verify if error is thrown
  }
  
  Test()
  verifyarithmeticexp(){
   ae = '+'
   verify no error is thrown
  }
  
  Test()
  verifyaddition(){
    num i = 3
    num j = 5
    char ae = '+'
    verify if res is 8
  }
 
  Test()
  verifysubtraction(){
    num i = 8
    num j = 4
    char ae = '-'
    verify if res is 4
  }
  Test()
  verifymultiplication(){
    num i = 10
    num j = 5
    char ae = '*'
    verify if res is 50
  }
  Test()
  verifydivision(){
    num i = 8
    num j = 4
    char ae = '/'
    verify if res is 2
  }
 }

 

What types of bugs are identified by unit testing

Unit testing validates the code flow against user requirements. It can find defects in methods or functions, in data flows and can also locate logical errors.

What are the advantages of unit testing

  • It helps in finding defects at the early stage of the software development life cycle. It prevents the defects from traversing to advanced stages of system development.
  • It reduces the cost to quality in the project as defects are fixed early.
  • Smallest possible unit of code is tested against user requirements; hence it helps to improve the quality and to build confidence in the software product.
  • Unit test cases serve as documentation to business analysts, management and product users.

What are the tools used for unit testing

JUnit, PHPUnit, NUnit, TestNG and HtmlUnit are some of the most commonly used frameworks for Unit Testing. Every language has different available tools for developers to develop unit tests easily.

Is unit testing alone sufficient to test the system completely

No, unit testing alone is not sufficient to test the system completely. It does not test the system as a whole and also does not test the connections between different parts of the software.  It does not test the performance of the system for load and stress testing.

[vc_row][vc_column width=”2/3″][td_block_text_with_title custom_title=”References”][/td_block_text_with_title][/vc_column][/vc_row]

https://en.wikipedia.org/wiki/Unit_testing

Translate »