What is unit testing and how important is it in the development cycle?
Software is an significant invention that help our life easier and more comfortable. We use software in every field, at any time and for many purposes: smart phone, smart home, automated manufacturing machines, medical devices, etc... There are several stages to the software development lifecycle to ensure the software suitable for the original purpose and no defect will be found in using, especially serious failure. So, the software must go through the testing phase before the production release. Software testing, which has many test levels will verify that software is working correctly or not. In this article I will introduce about unit testing, the basic level in software testing, and the role of it in the software development cycle.
What is unit testing?
Before learning about unit testing, let's go through the most basic concepts of software life cycle and software testing.
Software testing
Software development life cycle
Software Development Life Cycle is a process that follows for a software project, within a software organization. It includes a diagram that describes how to develop, maintain, change or upgrade specific software .
Software Development Life Cycle defines the complete cycle of development, such as all the tasks involved in planning, implementing, testing, and deploying a Software Product.
Software testing
Software, which does not work the way it should, can lead to many problems, some of them are actually waste of money and time fixing bugs, and even could affect reputation. Software testing is an effective way to verify that the software is as it was originally designed or not.
Software testing is a term used to refer to the process of software testing. This process is done for the purpose of finding bugs in the software before it is released to the market. Software testing can also be seen as the process of validating and verifying that the software program, application or product has met the technical, business and operational requirements as expected.
Unit testing starts when the coding is complete. In this phase, the developer or sometimes the tester, conduct design, implement, test and confirm its behaviors are in accordance with requirements, if something is wrong or unsuitable for user then further actions can be discussed with its developer.
Test levels
Test level is a set of organized and interconnected testing activities. Test levels are also related to the phases in the software development process.
There are many different testing levels in a development cycle, which help to check not only behavior but also the usability, performance of the software testing. Each completed test level imply that the software is gradually forming step by step.
In general, from basic to advance, there are four levels of testing: unit testing, integration testing, system testing, and acceptance testing. All levels have the only one purpose: recognize missing areas, which is not matched with the specification. So, we have to integrate each level to reach the most effecting in testing activities.
Unit testing
Unit testing is the act of testing at a basic level, testing each small module in the system, for example: functions, classes, methods. Unit testing can include functional and non-functional testing, such as resource behavior (e.g., looking for memory leaks) or load testing as well as structural testing (e.g., coverage decision example). Implement unit testing helps to check that component is satisfying requirement or not. This kind of testing is usually performed by the developer who created it, sometimes by the tester, in manual or automation way.
How important of unit testing in development cycle?
In order to answer the question “How important of unit testing in development cycle?”, let's see what benefits that unit testing distribute to software testing.
Advantages of unit testing
Unit testing is performed first in the testing process, to check at an early stage of the test process whether the independent functions work according to the specifications and whether there are any omissions in the specifications, to minimize rework at the later test level and help to reduce the cost of fixing software glitches.
Unit testing also help to prevent defects from escaping to higher test levels, this performs expression of Early testing principle, which will be detailed in next section, in software testing. In some cases, especially in incremental and iterative development models (e.g., Agile project) where code changes are ongoing, automated regression tests play a key role in building confidence that changes have not broken existing components.
Early testing
Software testing should start early in the Software Development Life Cycle. This helps to capture and eliminate defects in the early stages of Software Development Life Cycle such as requirement gathering or design phases. Early testing helps to reduce the number of defects and ultimately the rework cost in the phase later. Defects identified later in SDLC are expensive to fix than defects identified in early stage. So, testing should start early to avoid the introduction of defects in the early phase.
In traditional project, for example waterfall, testing phase often take place at last, and if a critical defect is found, it would be impact on release. For agile projects, when requirements change frequently, regression testing is mandatory, and if not early testing, it is certain that when conducting integration testing, it is easy to generate errors because of unit testing level that force developers to have to rework. Therefore, test planning should be started from beginning of the project and ensure that testing becomes part of the development process.
Unit testing techniques
The unit testing techniques are mainly categorized into three parts which are black box testing that involves testing of user interface along with input and output, white box testing that involves testing the functional behavior of the software application and gray box testing that is used to execute test suites, test methods, test cases and performing risk analysis.
Below are some code coverage techniques used in unit testing:
Statement Coverage
Decision Coverage
Branch Coverage
Condition Coverage
Which tools support in unit test?
Both manual test and automated test can be included in unit testing. Developer does not favor one over the other, but automation is preferred. By using unit testing tools, we can cover the maximum coverage, performance, compatibility, and integration testing. Unit testing tools are used by the developers to test the source code of the application or to achieve the source code of the application. A manual approach to unit testing may employ a detailed instructional document.
There are several automated unit test software available to assist with unit testing, below is some examples:
Junit
Junit is an open-source Unit testing tool used for Java programming language. It provides the assertions for testing expected results. JUnit helps developers can create a program that enhances the quality of the code and reduces the time of the debugging.
JMockit
JMockit is an open-source Unit testing tool, which is considered to replace the regular use of the mock object. This tool offers Line coverage, Path Coverage, and Data Coverage. Developers can use JMockit with collections of tools and API to create test using TestNG or Junit.
NUnit
NUnit is a unit-testing framework for the .NET programming language ported from Junit. NUnit has two forms, console and GUI. In fact, NUnit is often used in conjunction with CruiseControl.NET and used for automated testing on the build server, so programmers do not need to download anything.
TestNG
Test Next Generation (TestNG) is an open-source tool, which is stimulated from JUnit and NUnit testing frameworks. It has some new functionalities like Parallel Execution, Group Execution, Html Report, and Listener, so make TestNG more powerful. This tool supports Java and .Net programming languages.
Mentioned tools above are just a few of the popular unit testing tools. There are lots more, especially for C languages and Java, and I sure that you will find a unit testing tool for your programming needs regardless of the language you use.
Summary
Although has many advantages, unit testing also has some of disadvantages, such as:
- Unit testing is impossible to catch all error in a program.
- At higher level its might not find out integration or system level errors.
Thus, it’s recommended that unit testing should be used in conjunction with other testing activities in order to get best testing efficiency.