Unit testing is a software development process.

Data Testing: Test data is the data that is used in tests of a software system. In order to test a software application you need to enter some data for testing most of the features. Unit Testing: Unit testing, a testing technique using which individual modules are tested to determine if there are any issues by the developer himself. It is concerned with functional correctness of the standalone modules.

Data Testing:

Test data is the data that is used in tests of a software system. In order to test a software application you need to enter some data for testing most of the features. Any such specifically identified data which is used in tests is known as test data. ... Some test data is used to confirm the expected result.Test data is actually the input given to a software program. It represents data that affects or is affected by the execution of the specific module. Some data may be used for positive testing, typically to verify that a given set of input to a given function produces an expected result.Test data is data which has been specifically identified for use in tests, typically of a computer program. Some data may be used in a confirmatory way, typically to verify that a given set of input to a given function produces some expected result.

The GUI is in most cases given the most emphasis by the respective test managers as well as the development team members since the Graphical User Interface happens to be the most visible part of the application. However what is also important is to validate the information that can be considered as the heart of the application aka DATABASE.

Let us consider a Banking application whereby a user makes transactions. Now from database Testing viewpoint following things are important:

  1. The application stores the transaction information in the application database and displays them correctly to the user.
  2. No information is lost in the process.
  3. No partially performed or aborted operation information is saved by the application.
  4. No unauthorized individual is allowed to access the users information.

To ensure all these above objectives, we need to use data validation or data testing.This type of testing is also known as Back-end Testing or data testing.This type of testing chiefly deals with all the testable items that are generally hidden from the user for viewership. These include internal process and storage like Assembly, DBMS like Oracle,SQL Server, MYSQL, etc.This type of testing involves validatingthe schema,database tables,columns ,keys and indexes,stored procedures,triggers ,database server validations,validating data duplication.The tester in order to be able to perform back-end testing must have a strong background in the database server and Structured Query Language concepts.The 3 types of Database Testing are

  1. Structural Testing
  2. Functional Testing
  3. Non-functional Testing

Unit Testing:

Unit Testing of software applications is done during the development (coding) of an application. The objective of Unit Testing is to isolate a section of code and verify its correctness. In procedural programming, a unit may be an individual function or procedure. Unit Testing is usually performed by the developer.

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.

Why Unit Testing?

Sometimes software developers attempt to save time by doing minimal unit testing. This is a myth because skipping on unit testing leads to higherDefect fixing costs during System Testing, Integration Testing and even Beta Testing after the application is completed. Proper unit testing done during the development stage saves both time and money in the end. Here, are key reasons to perform unit testing.

  1. Unit Tests fix bug early in development cycle and save costs.
  2. It helps understand the developers the code base and enable them to make changes quickly
  3. Good unit tests serve as project documentation
  4. Unit tests help with code re-use. Migrate both your code and your tests to your new project. Tweak the code till the tests run again.

How to do Unit Testing?

Unit Testing is of two types

  • Manual
  • Automated

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 Example: Mock Objects

Unit testing relies on mock objects being created to test sections of code that are not yet part of a complete application. Mock objects fill in for the missing parts of the program.For example, you might have a function that needs variables or objects that are not created yet. In unit testing, those will be accounted for in the form of mock objects created solely for the purpose of the unit testing done on that section of code.

Unit Testing Tools:

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

  1. Jtest: Parasoft Jtest is an IDE plugin that leverages open-source frameworks (Junit, Mockito, PowerMock, and Spring) with guided and easy one-click actions for creating, scaling, and maintaining unit tests. By automating these time-consuming aspects of unit testing, it frees the developer to focus on business logic and create more meaningful test suites.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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. 

Test Driven Development (TDD) & Unit Testing:

Unit testing in TDD involves an extensive use of testing frameworks. A unit test framework is used in order to create automated unit tests. Unit testing frameworks are not unique to TDD, but they are essential to it. Below we look at some of what TDD brings to the world of unit testing:

  • Tests are written before the code
  • Rely heavily on testing frameworks
  • All classes in the applications are tested
  • Quick and easy integration is made possible

Unit Testing Techniques:

Techniques are:

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

Statement Coverage:

Statement coverage is a white box test design technique which involves execution of all the executable statements in the source code at least once. It is used to calculate and measure the number of statements in the source code which can be executed given the requirements.

Statement coverage is used to derive scenario based upon the structure of the code under test.

In White Box Testing, the tester is concentrating on how the software works. In other words, the tester will be concentrating on the internal working of source code concerning control flow graphs or flow charts.

Generally in any software, if we look at the source code, there will be a wide variety of elements like operators, functions, looping, exceptional handlers, etc. Based on the input to the program, some of the code statements may not be executed. The goal of Statement coverage is to cover all the possible path's, line, and statement in the code.

Decision Coverage:

Decision coverage reports the true or false outcomes of each Boolean expression. In this coverage, expressions can sometimes get complicated. Therefore, it is very hard to achieve 100% coverage.That's why there are many different methods of reporting this metric. All these methods focus on covering the most important combinations. It is very much similar to decision coverage, but it offers better sensitivity to control flow.

Branch Coverage:

In the branch coverage, every outcome from a code module is tested. For example, if the outcomes are binary, you need to test both True and False outcomes.It helps you to ensure that every possible branch from each decision condition is executed at least a single time.By using Branch coverage method, you can also measure the fraction of independent code segments. It also helps you to find out which is sections of code don't have any branches.

The formula to calculate Branch Coverage:

Condition Coverage:

Conditional coverage or expression coverage will reveal how the variables or subexpressions in the conditional statement are evaluated. In this coverage expressions with logical operands are only considered.For example, if an expression has Boolean operations like AND, OR, XOR, which indicated total possibilities.Conditional coverage offers better sensitivity to the control flow than decision coverage. Condition coverage does not give a guarantee about full decision coverage

The formula to calculate Condition Coverage:

Finite State Machine Coverage:

Finite state machine coverage is certainly the most complex type of code coverage method. This is because it works on the behavior of the design. In this coverage method, you need to look for how many time-specific states are visited, transited. It also checks how many sequences are included in a finite state machine.

Advantages of Unit Testing:

  • Reduces Defects in the Newly developed features or reduces bugs when changing the existing functionality.

  • Reduces Cost of Testing as defects are captured in very early phase.

  • Improves design and allows better refactoring of code.

  • Unit Tests, when integrated with build gives the quality of the build as well.

  • Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit API.
  • Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly (i.e. Regression testing). The procedure is to write test cases for all functions and methods so that whenever a change causes a fault, it can be quickly identified and fixed.
  • Due to the modular nature of the unit testing, we can test parts of the project without waiting for others to be completed.

Disadvantages of Unit Testing:

  • Unit testing can't be expected to catch every error in a program. It is not possible to evaluate all execution paths even in the most trivial programs
  • Unit testing by its very nature focuses on a unit of code. Hence it can't catch integration errors or broad system level errors.