Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object. 

In SDLC, STLC, V Model, Unit testing is first level of testing done before integration testing. Unit testing is a Whitebox testing technique that is usually performed by the developer. Though, in a practical world due to time crunch or reluctance of developers to tests, QA engineers also do unit testing. 

How to execute Unit Testing 

In order to execute Unit Tests, developers write a section of code to test a specific function in software application. Developers can also isolate this function to test more rigorously which reveals unnecessary dependencies between function being tested and other units so the dependencies can be eliminated. Developers generally use UnitTest framework to develop automated test cases for unit testing. 

Unit Testing is of two types 

Unit testing is commonly automated but may still be performed manually. Software Engineering does not favor one over the other but automation is preferred. A manual approach to unit testing may employ a step-by-step instructional document. 

Under the automated approach- 

  • A developer writes a section of code in the application just to test the function. They would later comment out and finally remove the test code when the application is deployed. 
  • A developer could also isolate the function to test it more rigorously. This is a more thorough unit testing practice that involves copy and paste of code to its own testing environment than its natural environment. Isolating the code helps in revealing unnecessary dependencies between the code being tested and other units or data spaces in the product. These dependencies can then be eliminated. 
  • A coder generally uses a UnitTest Framework to develop automated test cases. Using an automation framework, the developer codes criteria into the test to verify the correctness of the code. During execution of the test cases, the framework logs failing test cases. Many frameworks will also automatically flag and report, in summary, these failed test cases. Depending on the severity of a failure, the framework may halt subsequent testing. 
  • The workflow of Unit Testing is: 

 1) Create Test Cases 

 2) Review/Rework 

 3) Baseline 

 4) Execute Test Cases. 

Unit Testing Techniques 

The Unit Testing Techniques are mainly categorized into three parts which are Black box testing that involves testing of user interface along with input and output, White box testing that involves testing the functional behaviour of the software application and Gray box testing that is used to execute test suites, test methods, test cases and performing risk analysis. 



Code coverage techniques used in Unit Testing are listed below: 

  • Statement Coverage 
  • Decision Coverage 
  • Branch Coverage 
  • Condition Coverage 
  • Finite State Machine Coverage 

Unit Testing Tools 

There are several automated unit test software available to assist with unit testing. We will provide a few examples below: 

  1. Junit: Junit is a free to use testing tool used for Java programming language.  It provides assertions to identify test method. This tool test data first and then inserted in the piece of code. 
  2. NUnit:  NUnit is widely used unit-testing framework use for all .net languages.  It is an open-source tool which allows writing scripts manually. It supports data-driven tests which can run in parallel. 
  3. JMockit:  JMockit is open-source Unit testing tool.  It is a code coverage tool with line and path metrics. It allows mocking API with recording and verification syntax. This tool offers Line coverage, Path Coverage, and Data Coverage. 
  4. EMMA:  EMMA is an open-source toolkit for analyzing and reporting code written in Java language. Emma support coverage types like method, line, basic block. It is Java-based so it is without external library dependencies and can access the source code. 
  5. PHPUnit: PHPUnit is a unit testing tool for PHP programmer. It takes small portions of code which is called units and test each of them separately.  The tool also allows developers to use pre-define assertion methods to assert that a system behave in a certain manner.  

Those are just a few of the available unit testing tools. There are lots more, especially for C languages and Java, but you are sure to find a unit testing tool for your programming needs regardless of the language you use.