What is unit testing?
Unit testing refers to the practice of testing certain functions and areas – or units – of our code. This gives us the ability to verify that our functions work as expected. That is to say that for any function and given a set of inputs, we can determine if the function is returning the proper values and will gracefully handle failures during the course of execution should invalid input be provided.
Unit testing frameworks, drivers, stubs, and mock/ fake objects are used to assist in unit testing.
Unit testing involves only those characteristics that are vital to the performance of the unit under test. This encourages developers to modify the source code without immediate concerns about how such changes might affect the functioning of other units or the program as a whole. Once all of the units in a program have been found to be working in the most efficient and error-free manner possible, larger components of the program can be evaluated by means of integration testing.
Unit testing can be time-consuming and tedious. It demands patience and thoroughness on the part of the development team. Rigorous documentation must be maintained. Unit testing must be done with an awareness that it may not be possible to test a unit for every input scenario that will occur when the program is run in a real-world environment.
Unit testing is essentially a set of path, test performed to examine the several different paths through the modules. Unit testing is remarkably done by programmers with the help of Unit framework (like J Unit, CPP Unit etc. depending up on the language source code is written). Unit testing is usually an automated process and performed within the programmers IDE.
Method used for unit testing
White box testing method is used for the unit testing.
When unit testing should performed?
Unit testing should be done before integration testing.
Who performs unit testing?
Developer itself performs unit testing but in rare cases it may also be performed by independent software tester.
Tasks involved in unit testing
- Unit test plan
- Unit test cases/ Scripts
- Unit test
Tests that are performed during the unit testing are explained below:
- Module Interface test: In module interface test, it is checked whether the information is properly flowing in to the program unit (or module) and properly happen out of it or not.
- Local data structures: These are tested to inquiry if the local data within the module is stored properly or not.
- Boundary conditions: It is observed that much software often fails at boundary related conditions. That’s why boundary related conditions are always tested to make safe that the program is properly working at its boundary condition’s.
- Independent paths: All independent paths are tested to see that they are properly executing their task and terminating at the end of the program.
- Error handling paths: These are tested to review if errors are handled properly by them or not.
Unit Testing Example
For example you are testing a function; whether loop or statement in a program is working properly or not than this is called as unit testing. A beneficial example of a framework that allows automated unit testing is JUNIT (a unit testing framework for java). XUnit  is a more general framework which supports other languages like C#, ASP, C++, Delphi and Python to name a few.
Advantages of Unit testing:
- Issues are found at early stage. Since unit testing are carried out by developers where they test their individual code before the integration. Hence the issues can be found very early and can be resolved then and there without impacting the other piece of codes.
- Codes are more reusable. In order to make unit testing possible, codes need to be modular. This means that codes are easier to reuse.
- Unit testing increases confidence in changing/ maintaining code. If good unit tests are written and if they are run every time any code is changed, we will be able to promptly catch any defects introduced due to the change. Also, if codes are already made less interdependent to make unit testing possible, the unintended impact of changes to any code is less.
- The cost of fixing a defect detected during unit testing is lesser in comparison to that of defects detected at higher levels. Compare the cost (time, effort, destruction, humiliation) of a defect detected during acceptance testing or when the software is live.
- Debugging is easy. When a test fails, only the latest changes need to be debugged. With testing at higher levels, changes made over the span of several days/ weeks/ months need to be scanned.
At last we conclude that Unit testing focuses on the distinct modules of the product and check the consistency of the every module.