unit testing

What is unit testing?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually scrutinized for proper operation. Software developers and sometimes QA staff complete unit tests during the development process. 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. If done correctly, unit tests can 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. Teams should perform unit tests frequently, either manually or more often automated.

How unit tests work

A unit test comprises three stages: planning, writing test cases and performing the unit test itself. In the first step, developers or QA professionals prepare and review the unit test. In the next step, they write test cases and scripts. In the third step 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 codebase.

Each test case is tested independently in an isolated environment to ensure a lack of dependencies in the code. The software developer should code criteria to verify each test case and use a testing framework 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 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 being tested. 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 the units in a program work in the most efficient and error-free manner possible, teams can evaluate larger components of the program by means of integration testing.

Manual vs. automated unit testing

Developers can perform unit tests manually or automatically. 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 for unit testing. 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.

Unit testing advantages

There are many advantages to unit testing, including the following:

  • The earlier a problem is identified, the fewer compound errors occur.
  • Fixing problems early is usually cheaper than fixing them later in development.
  • Easier debugging processes.
  • Developers can quickly make changes to the codebase.
  • Developers can reuse code and migrate it to new projects.

Unit testing disadvantages

While unit testing is integral to any software development and testing strategy, there are some aspects to be aware of. Disadvantages to unit testing include the following:

  • 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.
  • Developers may have to learn new skills to implement unit testing correctly; for example, having to learn how to use specific automated software tools.

Editor's note: This article was reformatted to improve the reader experience.

This was last updated in May 2023

Continue Reading About unit testing

Dig Deeper on Software testing tools and techniques

Cloud Computing
App Architecture