The Essence of Unit Testing

Introduction

Unit testing is a process where pieces of software are tested to verify if each piece of the code performs as expected. One or more unit tests are run as part of unit testing.

The pieces of software that are tested may be at any level of detail, including individual functions, methods, classes, or modules.

Perhaps a more formal definition of unit testing is as follows:

In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures—are tested to determine whether they are fit for use.

– Kolawa A.Automated Defect Prevention: Best Practices in Software Management, IEEE Press, 20072007.

Unit testing benefits

The general unit testing benefits are the same as those found in automated testing.

The advantage of unit testing is that any software change applied to a unit of application code can be retested with marginal effort. Therefore, unit test cases can be run after every change, immediately identifying potential software regression. This is in contrast to manual testing. In manual testing, the laborious process is repeated.

This advantage is very much in line with automated testing, but with an emphasis on testing code in units. A unit of software is any piece of code that consists of a function or functions in a procedural language or of a class and its nested classes in an object-oriented language.

Automated vs manual unit tests

Theoretically, unit tests may be both automated and manual. A class may be tested manually by invoking its method from a program’s execution path and observing its output on the console or some other frontend. In this case, this would be called a manual unit test.

The code below, which you should be familiar with from a previous lesson, consists of a MyMeasurements class. This class simply stores two lists of double data. One list stores the original amounts as provided and the other stores another copied list that stores each of the same values, but in thousandths. The MyMeasurements class is called from the Main method.

Run a manual unit test below to see if the Measurements lists are initialized with the data provided:

Get hands-on with 1200+ tech skills courses.