unit testing

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.  The main objective of unit testing is to isolate written code to test and determine if it works as intended.

Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.

Unit testing is a component of test-driven development (TDD), a pragmatic methodology that takes a meticulous approach to building a product by means of continual testing and revision. This testing method is also the first level of software testing, which is performed before other testing methods such as integration testing. Unit tests are typically isolated to ensure a unit does not rely on any external code or functions. Testing can be done manually but is often automated.

How unit tests work

A unit test typically comprises of three stages: plan, cases and scripting and the unit test itself. In the first step, the unit test is prepared and reviewed. The next step is for the test cases and scripts to be made, then the code is tested.

Test-driven development requires that developers first write failing unit tests. Then they write code and refactor the application until the test passes. TDD typically results in an explicit and predictable code base.

Each test case is tested independently in an isolated environment, as to ensure a lack of dependencies in the code. The software developer should code criteria to verify each test case, and a testing framework can be used to report any failed tests. Developers should not make a test for every line of code, as this may take up too much time. Developers should then create tests focusing on code which could affect the behavior of the software being developed.

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 tests should be performed frequently, and can be done manually or can be automated.

Types of unit testing

Unit tests can be performed manually or automated. Those employing a manual method may have an instinctual document made detailing each step in the process; however, automated testing is the more common method to unit tests. Automated approaches commonly use a testing framework to develop test cases. These frameworks are also set to flag and report any failed test cases while also providing a summary of test cases.

Advantages and disadvantages of unit testing

Advantages to unit testing include:                              

  • The earlier a problem is identified, the fewer compound errors occur.
  • Costs of fixing a problem early can quickly outweigh the cost of fixing it later.
  • Debugging processes are made easier.
  • Developers can quickly make changes to the code base.
  • Developers can also re-use code, migrating it to new projects.

Disadvantages include:

  • Tests will not uncover every bug.
  • Unit tests only test sets of data and its functionality—it will not catch errors in integration.
  • More lines of test code may need to be written to test one line of code—creating a potential time investment.
  • Unit testing may have a steep learning curve, for example, having to learn how to use specific automated software tools.
This was last updated in August 2019

Next Steps

What are the four major categories for application testing tools?

Learn about use cases for three categories of application testing tools: automation, bug tracking and coverage.

Continue Reading About unit testing

Dig Deeper on Software testing tools and techniques

Cloud Computing
App Architecture