Unit testing is performed on the smallest testable part of the application like methods, class etc. A Unit test is mainly carried out by developers or can also be carried by software tester. This mainly involves writing the test case of application in the same language which is used for development or there are some framework that provides the structure to write the test case and execute them effectively like we have PHP Unit in PHP for writing unit test cases. These are thus programs written in batches or test files and are verified with some input and with their expected output. A unit test is a key component of software programming, its permit the easy code changes with minimal time for the test at later stages of development.
Why is unit testing important?
Suppose you develop two module unit1 and unit2 and you neither performed unit test nor integration test and you keep modifying them. At the time of final delivery of product you found an issue, now where can the issue be:-
- It could be a bug with unit1
- It could be a bug with unit2
- It could be a bug with both unit1 and unit2
- It could be a bug in the interface that manages unit1 and unit2
Now you need to debug all your code, but if a unit test was carried out these issues can have resolved earlier (might be they never appeared), or at least you would have known where the issue is, by executing your test cases.
Unit testing is often neglected but it is the most important part of testing especially in a big project where multiple developers are working on the various module. Unit test is the first level of testing and is performed before integration test can be done, it is performed by using white box testing method.
Pros of Unit testing:-
- It helps in finding a bug at an early stage of development and also helps to recognize if there is any flaw in the implementation of the specification.
- The process of writing test cases forces a developer to think about the multiple scenarios of errors with various inputs and output conditions. Thus it also forces a developer to write function and object in a structured way, making code reusable and more modular.
- Development is faster initially, you may find it tedious or may feel that writing the test case is an extra overhead for you but later on when you make any modification in your code and wants to test then you need not enter various input combination again and again, just you need is to execute your test case. Efforts required to fix a bug during the unit testing is less in comparison to that of defects detected at a higher level.
- Debugging is easy, as already given example above unit test makes debugging easy for you by providing you the precise region or module where an error is related too.