Unit Testing: What It Is and How It’s Done

作者:Coursera Staff • 更新于

Discover the ins and outs of unit testing, a part of a broader, more extensive software testing regimen that supports software efficacy and reliability.

[Featured Image] Two software developers discuss unit testing in front of a computer screen in an office setting with other colleagues in the background.

Key takeaways

Unit testing closely examines an application's smallest functional components, commonly called units, and tests them for efficiency.

  • In 2025, 45 percent of US companies incurred costs over $5 million annually due to low-quality software, signifying the importance of unit testing [1].

  • While unit testing can increase the chances of finding bugs early in the software development cycle, unit tests may not reveal all errors in code.

  • You can use unit testing in your role as a software developer, programmer, or QA tester.

Discover more about the basics of unit testing, including its benefits, best practices, and information on writing unit test cases. If you’re ready to start learning more about software engineering, enroll in the IBM DevOps and Software Engineering Professional Certificate. You’ll have the opportunity to learn more about creating applications with Python, using automation tools for efficiency, and more in as little as six months. Upon completion, you’ll have earned a career credential for your resume.

What is unit testing in software?

Unit testing closely examines an application's smallest functional components, commonly called units. These components are each tested for efficiency. 

The arrange, act, assert (AAA) pattern is a widely used approach for writing unit tests. During the arrange phase, an isolated or individual component of an application that requires testing (known as the system under test or SUT) is initialized. The second phase, the act phase, involves probing the SUT with targeted test conditions. Finally, in the assert phase, the resulting behavior of the SUT is verified.

The unit test succeeds if the observed behavior aligns with the expected outcome. A failure implies that a problem within the system under test might exist. 

Unit tests are specifically designed to test code that is free of external dependencies, such as interactions with the database or web services.

Who performs unit testing?

Software developers are typically in charge of conducting unit testing, although quality assurance (QA) engineers may also take part in performing unit tests. As a norm, developers pursue testing during the development phase of an application or software. Developers and programmers need to be adept at unit testing because it improves the quality of their products, meaning that their projects have fewer errors and take less time to complete.

Did you know? A Python library by the name of “pytest” lets you run unit tests within a program. Its other interesting feature includes the auto-discovery of test modules and functions.

The significance of unit testing in software development

Software bugs can have significant negative ramifications. In 2025, 45 percent of US companies incurred costs over $5 million annually due to low-quality software, while 81 percent of the global surveyed companies reported losses between $500,000 and $5 million every year, according to data sourced from Tricentis [1].

Bugs that go undetected can be expensive to fix, as is the cost of identifying and creating solutions for software defects. More importantly, software flaws can compromise end-user experience, resulting in customer attrition. Software testing is a proactive approach to identifying and addressing software defects, from bugs to system failures.

Testing is seldom haphazard or arbitrary. In fact, software testing adheres to a cycle. Before general release, an application undergoes four stages of testing: unit testing, integration testing, system testing, and acceptance testing. 

Unit testing serves as the basis for all subsequent tests, which means the quality and comprehensiveness of unit testing significantly impact the effectiveness of other tests and the overall performance of an application.

Types of software unit testing

Manual unit testing and automated unit testing are two variants of unit testing. Below is an overview of each.

Manual unit testing

In manual unit testing, developers test a specific section of an application by manually interacting with the source code. Developers may add or remove lines of code to gauge units’ efficiency. This process can be expensive and time-consuming, as isolating and testing independent units becomes increasingly difficult as code complexity grows. 

Automated unit testing 

Automated unit testing is faster, less expensive, and requires minimal developer intervention. This type of testing isolates code for independent testing by duplicating the code in a testing environment, thereby exposing any dependencies between code units. Automated unit tests are usually written as code snippets integrated into the software. The embedded testing code is erased before software deployment.

Read more: How to Become a QA Automation Tester

Pros and cons of unit testing

Unit testing has several merits. However, it also has limitations. The following are a few notable pros and cons of unit testing.

Advantages of unit testing

  • Since unit tests focus on small, isolated code modules, pinpointing the source of an error is fairly straightforward.

  • Unit tests also provide documentation for the code, allowing for better collaboration between developers and project coordinators.

Disadvantages of unit testing

  • Unit tests may not reveal all errors in code.

  • Training is necessary for developers to perform unit testing correctly and efficiently.

  • Constant updates to unit tests may occur as code changes and evolves, increasing time involvement.

Unit testing tools

Tools are an integral part of automation testing. The following lists some commonly used open-source automated unit testing tools.

1. JUnit

The JUnit platform provides a base for launching testing frameworks on the Java Virtual Machine (JVM). The current version, JUnit 6, is compatible with the Java 17 platform and higher. 

JUnit 6 aims to separate the inner workings of test discovery and execution from the external filtering and configuration required by IDEs, as well as build tools, through a launcher that enables the discovery, filtering, and execution of tests. A custom TestEngine allows test libraries like Spock, Cucumber, and FitNesse to integrate with JUnit.

2. NUnit

NUnit is an open-source unit testing framework for all .NET languages. NUnit was originally adapted from JUnit, but the current release has undergone a complete rewrite and now offers many new features, along with support for a wide range of .NET platforms. Custom attributes help identify tests in NUnit.

3. PHPUnit

PHPUnit is a framework for unit testing in PHP. An instance of the xUnit architecture for unit testing, PHPUnit supports various approaches to structuring, organizing, and combining tests into a test suite. PHPUnit can also assist in identifying risky tests, such as a false-positive test that may provide a false sense of code stability. 

Did you know? NASA employs assertion-based unit test tools to examine flight software.

Uncover practical guides for your career journey 

Discover fresh insights into your career or learn about trends in your industry by subscribing to our LinkedIn newsletter, Career Chat. Or if you want to keep learning more about the field of software engineering, check out these free resources:

Whether you want to develop a new skill, get comfortable with an in-demand technology, or advance your abilities, keep growing with a Coursera Plus subscription. You’ll get access to over 10,000 flexible courses. 

文章来源

  1. Tricentis. “2025 Quality Transformation Report, https://be.tricentis.com/media-assets/pdf/Tricentis-report_Tricentis-2025-quality-transformation-report.pdf.” Accessed February 5, 2026.

更新于
作者:

编辑团队

Coursera 的编辑团队由经验丰富的专业编辑、作者和事实核查人员组成。我们的文章都经过深入研究和全面审核,以确保为任何主题提供值得信赖的信息和建议。我们深知,在您的教育或职业生涯中迈出下一步时可能...

此内容仅供参考。建议学生多做研究,确保所追求的课程和其他证书符合他们的个人、专业和财务目标。