The art of unit testing explained
Swayam Prakasha explores and explains one of the most important methodologies in modern software development…
Here are some of the entry and exit criteria for unit testing…
Entry criteria
- Business requirements are at least 80% complete and have been approved to date.
- Technical design has been finalised and approved.
- Development environment has been established and is stable.
- Code development for the module is complete.
Exit criteria
- Code has version control in place.
- No known major or critical defects prevent any modules from moving to system testing.
Let’s take a look at test plan and test case documents, which are the major deliverables in any type of testing. A test plan is a formal and detailed document and typically it includes the following…
- Scope, objective and testing approach.
- Allocated resources (both people and other hardware / software requirements).
- Risks and dependencies.
- Test entry and exit criteria.
- Reporting and communication process to be followed.
- Schedule and various milestones.
A test case document is the one that defines a test item and specifies a set of test inputs or data, execution conditions and expected results. A test case is generally executed manually, but many test cases can be combined for automated execution. A unit test can be pictorially represented by a data flow diagram. As can be seen from the diagram below, a developer uses the source code and test plan to accomplish unit testing. The result of this activity is an artefact: the test results. These results will then be reviewed and the result of this activity should be an approval of unit testing. There are some ground rules that determine whether a test we are doing falls into the unit testing category or not. Generally speaking, a test is not a unit test if it talks to the database or communicates across the network.
Quick unit testing tips
Software developers can always follow certain tips while doing unit testing. Let’s take a look at some of these tips…
Use unit tests to manage the associated risks – unit testing is considered as a continuous software testing. When every unit test succeeds, we can be sure that the unit is working as expected. Effective utilisation of unit tests will make sure that we reduce the risks associated with untested code, if any.
Always write smart tests – unit testing is a time-consuming, tedious process. Thus there is a need to make sure that your unit tests are very effective. Make sure that you focus on testing the core functionality of each unit.
Have a clean environment for each test – it is very important to set up each test correctly. You should avoid situations where your test failed because it used incorrect data. And typically, we need not worry about efficiency while doing unit testing.
Always write tests before fixing a bug – when you discover a bug, make sure that you write a unit test case before that bug gets fixed. This will make sure that we will be able to catch the bug if it reappears.
Run tests continuously – software developers need to run unit tests a lot. It is good to make sure that you run these tests every few hours or once a day (as the need may be). It is always a good practice to automate unit tests and run them continuously.
So what does a unit test test? This is an important question that needs to be answered by the software developers – and at the same time, it is most difficult to answer. In simple words, we can say that the unit test verifies that the requirements are being met. When we say requirements, we need to keep in mind customer requirements and functional requirements. Various frameworks are available in the market for carrying out unit testing. One of the popular frameworks is JUnit. Readers can learn more about this here.
It is important to understand that unit testing involves only those characteristics that are critical to the performance of the unit under test. With this, developers are encouraged to modify the source code without immediate concerns about how such changes might affect the functioning of other units or the system as a whole. Once all of the units or modules in a system have been found to be working in the most efficient and error-free manner possible, we can proceed to the next level of testing, such as integration testing and system testing.
To obtain the intended benefits from unit testing, we need to have a rigorous sense of discipline throughout the software development process. It is essential to keep careful records, not only of the tests that have been performed, but also of all changes that have been made to the source code of this or any other unit in the software. Use of a version control system is very essential. If a later version of the unit fails a particular test that it had previously passed, the version control software can provide a list of the source code changes (if any) that have been applied to the unit since that time.
It is also essential to implement a sustainable process for ensuring that test case failures are reviewed daily and addressed immediately. If such a process is not implemented and ingrained into the team’s workflow, the application will evolve out of sync with the unit test suite – increasing false positives and reducing the effectiveness of the test suite.
In conclusion
Software testing is considered to be a critical element in the software development life cycle and has the potential to save time and money by identifying problems in the early stages and to improve customer satisfaction by delivering a more defect-free product. Testing, in general, cannot be expected to catch every error in the program. The same is also true for unit testing. By definition, it only tests the functionality of the units themselves.
Therefore, it may not catch integration errors, performance problems or other system-wide issues. Unit testing is more effective if it is used in conjunction with other software testing activities. Developers always feel that unit testing is pretty time-consuming and tedious. It calls for great patience and thoroughness on the part of the development team. It is essential to maintain good documentation. 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.
















Trackbacks
What's your opinion?