Importance of Unit Testing
Tags: androidunittesting, automatingtesting, unittesting
Published: 2019-05-16 05:05:41

---
title: Importance of Unit Testing
published: true
description: Unit testing is the first level where most of the issues can be rectified and this saves time. 
tags: Android Unit Testing, Automating Testing, Unit Testing
canonical_url: https://www.pcloudy.com/importance-of-unit-testing/
---
We all know that testing your application is important as it ensures security, customer satisfaction, and saves money in the long run. Sometimes it can saves lives as well. Here is an example to make my point; China Airlines plane crashed due to a software bug on April 26, 1994, which took 264 lives. In software testing, Unit testing is the first level where most of the issues can be rectified and this saves time. Let’s understand what is Unit testing.
<img src="https://www.pcloudy.com/wp-content/uploads/2019/05/level-of-testing.png">

<b>What is Unit testing?</b>
In Unit Testing, individual components of a software application are tested during the development phase. Unit Testing is usually done by developers instead of testers. A function which is in the form of a section of code is tested to verify the accuracy. Drivers, unit testing frameworks, mock objects, and stubs are used to perform unit testing.

<b>How to Perform Unit Testing</b>
Unit testing is usually Automated, but sometimes it can be done manually. A manual approach can be done with the help of an instructional document. Unit testing can be performed on all <a href="http://www.pcloudy.com/types-of-mobile-apps/">types of mobile applications</a>.
 
In automated unit testing, a developer writes code in the app to test the function or procedure. Once the app is deployed, that code can be removed. The function can be isolated to test the app rigorously and it reveals the dependencies between the code being tested and other units. Then the dependencies can be eliminated. Most of the developers use unit test automated framework to log the failing test cases.
<img src="https://www.pcloudy.com/wp-content/uploads/2019/05/unit-test-life-cycle.png">

<b>How to Improve Unit Testing</b>
There are some points to keep in mind while performing unit testing. Use consistent naming conventions and test one code at a time. Make sure that there is a corresponding unit test case for a module if there is any change in the code. All the bugs must be fixed before moving to the next phase. It’s better to test as you commit a code to avoid errors. Focus more on the tests that affect the behavior of the system. <a href="https://www.pcloudy.com/problems-with-online-android-emulator-and-how-to-solve-it/">Online Android emulators</a> can be used to perform unit testing in some cases.

<b>Advantages and Disadvantages of Unit Testing</b>
 
With Unit Testing, the speed of development will be faster. If you perform developer test instead of unit test then you need to set breakpoints, fire up the GUI and provide inputs. But if you do unit test then, you write the code, write the test and then run the test. You don’t need to provide the inputs or fire up the GUI. In the end, you have a more reliable code. It takes less time to find and fix the bugs during unit testing than in system or Acceptance testing.
 
Reusable codes reduce the effort and save time as the code needs to be modular in unit testing.
 
More reliable code as the bugs are fixed in the initial stage.
 
Easy to debug as only the latest changes need to be debugged if the test fails. Whereas if you test on a higher level, then you will have to scan the changes made within weeks or months.

We can test a part of the project without waiting for others to be completed due to the modular nature of the unit test.
 
The only disadvantage with Unit Testing is that it’s not possible to check all the execution paths and it cannot catch broad system errors or integration errors.
 
<b>Conclusion</b>
It is evident that unit testing cannot and should not be avoided. Rather the developers should opt for Test-driven development where they can write the test and then write the code on the basis of that test. You just need to get the suitable tools which will further reduce your testing efforts.