Skip to main content

Section 2.5 Unit Testing

Subsection 2.5.1 Why do we need unit testing?

Software evolves over time. Thus, we should adapt to change rather than stick to a strict plan. Software development is a collaborative process: many individuals work on different parts of the software to build something that meets customer needs, and different individuals perform different roles iteratively to determine a product’s future. We work with new technologies, ever-changing requirements, people movement, or a combination of these. All of these determine that software projects involve a lot of uncertainties. People say that the only constant is uncertainty. To overcome the fear and manage these uncertainties, we need a software development practice that can help us produce working software. It must keep things simple and provide us quick feedback in case things go wrong. We need to verify the working software when change happens.
Test-driven development (TDD) is one of the practices of Agile software development that a lot of developers use in some shape or form. The premise of TDD is that you write a failing test case before you write the production code itself. TDD, if done correctly, can help you write software that meets customer expectations, has a simple design, and has fewer defects.

Subsection 2.5.2 What is Unit Testing?

The main idea of unit testing is testing software with a small piece of source code (unit, component, and/or function) of the same software. "Unit testing" means that the software consists of "units" which are separate testable parts of the product. An individual program, class, method, function etc. can be such "unit". Unit testing allows checking whether a unit behaves as the developer intended and whether a unit corresponds to the design specifications. Unit testing provides an ability of independent testing for each software unit.
The advantages of unit testing is as follows:
  • Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit API.
  • Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly (i.e. Regression testing).
  • The procedure is to write test cases for all functions and methods so that whenever a change causes a fault, it can be quickly identified and fixed. Due to the modular nature of the unit testing, we can test parts of the project without waiting for others to be completed.

Subsection 2.5.3 What is JUnit?

JUnit, developed by Kent Beck and Erich Gamma, is one of the most popular unit-testing frameworks for Java developers. It was originally based on SUnit, a unit-testing framework written in Smalltalk (developed by Kent Beck). The first version of JUnit was released in 1997.
Prior to the introduction of JUnit, testing discipline was dominated by capture and replay testing tools. These tools were black-box testing tools, which used to capture the state of the system with a given input and then try to replay it. The tests written in such a framework involved an enormous amount of effort. These tools were not designed to unit test a component as they tested the application using its graphical user interface (GUI).
JUnit rejected the idea of GUI-based tests. It instead provided a lightweight framework, which enabled test creation by writing code in Java. This allowed developers to build test suites for every piece of their code. Due to its benefits, JUnit was integrated with all kinds of build tools and integrated development environments (IDEs).
JUnit is a unit testing framework for Java programming language. It plays a crucial role test-driven development, and is a family of unit testing frameworks collectively known as xUnit. JUnit promotes the idea of "first testing then coding", which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented. This approach is like "test a little, code a little, test a little, code a little." It increases the productivity of the programmer and the stability of program code, which in turn reduces the stress on the programmer and the time spent on debugging.
You have attempted of activities on this page.