Effective Strategies of Unit Testing
Place of Unit Testing in Software Development Life Cycle:
This is the first and the most important level of testing. Its need begins from the moment a programmer develops a unit of code. Every unit is tested for various scenarios. Detecting and fixing bugs during early stages of the Software Lifecycle helps reduce costly fixes later on. It is much more economical to find and eliminate the bugs during early stages of application building process. Hence Unit Testing is the most important of all the testing levels. As the software project progresses ahead it becomes more and more costly to find and fix the bugs.
By and large developers carry the prime responsibility to deliver Unit Tested Code.
Steps for Unit Testing are:
Step 1: Creation of a Test Plan
Step 2: Creation of Test Cases and the Test Data
Step 3: Creation of scripts to run the test cases wherever applicable
Step 4: Execution of the test cases, once the code is ready
Step 5: Fixing of the bugs if present and re testing of the code
Step 6: Repetition of the test cycle until the Unit is free from all types of bugs
Unit Test Plan:
Unit Test Plan Is a document, which describes the Test Plan. It describes various steps as to how the tests will be carried out. This typically includes the list of things to be Tested, Roles and Responsibilities, prerequisites to begin the Testing, Test Environment, Assumptions, what to do after a test is successfully carried out, what to do if test fails, Glossary and so on
What is a Test Case:
Test Case is a document which describes exactly how the test should be carried out.
For example the test case may describe a test as follows:
Step 1: Type 10 characters in the Name Field
Step 2: Click on Submit etc. etc. & so on
When many Test Cases are clubbed together they form a Test Suite
Broad Format of a Test Case is as under
Test Case ID | Test Case Desc. | Input Data | Desired Result |
Actual Result | Pass Fail | Result |
In addition to the above, following information may also be captured & included in the Test Case format:
- Name of the Unit
- Version Number
- Tested By
- Date
- Test Iteration (More than one iterations of unit testing may be performed as per the need)
Steps to Effective Unit Testing:
1) Proper Documentation: Right from the beginning, do proper documentation of all the Test Cases as planned to unit test the code. Many a times documentation is not given due importance. It is very important to do documentation of the Test Cases, actual Results when executing the Test Cases, Response Time of the code for each test case. If all the test cases and the actual execution of test cases are well documented, we derive several significant advantages like.
- Proper documentation of the Test Cases prevent oversight.
- Proper documentation of the Test Cases reflects towards the quality of test cases.
- If the code needs to be re-tested we can be sure that we did not miss anything.
- Proper documentation provides a good level of transparency regarding the real contents during the unit testing. This is one of the most important aspects.
- Proper documentation helps in knowledge transfer in case of programmer attrition.
- Sometimes Unit Test Cases can be used to develop test cases for other levels of testing. In that case, if proper documentation is available, it is extremely helpful.
An Effective Unit Testing Process should always be developed to increase the Software Reliability and credibility of the developer. Many new developers consider unit testing tasks lightly but sooner or the later realize its importance down the road across SDLC.
2) Areas to be covered during Unit Testing: There is no specific hard & fast rule for the areas of Unit testing. A lot depends on the type of program or unit which is being created. It could be a screen or a component or a web service. Broadly speaking following aspects can be considered:
- In case of a UI screen, have test cases to verify all the screen elements which are expected to appear on the screens.
- In case of a UI screen, have test cases to verify the spellings / font / size of all the Labels or text which appears on the screen.
- Create Test Cases in such a way that every line of code in the unit gets tested at least once in a test cycle
- Create Test Cases in such a way that every condition in case of – Conditional Statements gets tested at least once.
- Create Test Cases to test the minimum / maximum range of data which can be entered. For instance what shall be the maximum – Amount which can be entered or the max length of string which can be entered or passed in as a parameter.
- Create Test Cases to verify how various errors are handled.
- Create Test Cases to verify if all the validations are being performed.
3) Do Necessary Automation if need be: Developers are usually seen cutting corners in unit testing due to time pressures to complete the task. Sometimes it helps to write scripts, which automate a part of unit testing. This may help ensure that the necessary tests were done and may result in saving time required to perform the tests.
Many More Articles on Best Practices in Manual Testing
An expert on R&D, Online Training and Publishing. He is M.Tech. (Honours) and is a part of the STG team since inception.