Unit testing is one of the software testing methodologies, where single units of code, modules, and components are tested individually and independently.
What is the Purpose?
The purpose of unit testing to test the individual units before actually putting them to work.
Example of Unit Testing:
You have a program with four units, and there is some bug in the code. To determine where this bug is you will have to go through these four individual units and get them tested and you will also have to check whether the testing is done properly.
What is a Good Unit Test?
A good unit test is:
- Able to be fully automated
- Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
- Can be run in any order if part of many other tests
- Runs in memory (no DB or File access, for example)
- Consistently returns the same result (You always run the same test, so no random numbers, for instance. Save those for integration or range tests)
- Runs fast
- Tests a single logical concept in the system
- Trustworthy (when you see its result, you don’t need to debug the code just to be sure)
A best unit testing practice focuses on verification effort of the smallest unit of software design, i.e., the software component or module.
What is done in Unit Testing – Its Process
Here are the following tests which are carried out in software unit testing:
Local data structures are examined to ensure that data stored temporarily maintains its integrity during all steps in an algorithm’s execution.
All independent paths through the control structure are exercised to ensure that all statements in a module have been executed at least once.
Boundary conditions are tested to ensure that the module operates correctly, boundaries established to limit or restrict processing, and all error handling paths are tested.
Unit test procedures – Unit testing is normally considered as an adjunct to the coding step. The design of unit tests can be performed before coding begins or after source code has been generated. A review of design information guides establishing test cases that are likely to uncover errors in each of the categories discussed earlier. Each test case should be coupled with a set of expected results.
Benefits Of Unit Testing:
- It will speed up your development, since you don’t need to build the whole project.
- It will make you “fail fast”, which follows the agile methodology
- It will give to the confidence to refract your code.
- It will act as specification for the next guys who is going to maintain/develop your code.
- If you go a one step further and follow TDD (Test Driven Development), tests can be used as a design tool to make your code focus of the features of the program and cut off the unnecessary boilerplate.
Know more & Get in the depth of testing:
- What is Software Testing?
- Integration Testing: A process followed after Unit Testing
- Basic Unit Testing Checklist – Download File