To Unit Test or not to Unit Test? That is the question!

This is a response article to “Software testing: an overlooked process during software development”[8] by s1222207.

Compared to past years, one could say that there is an increase in the size of software projects as well as in their complexity. The reason for that is that the needs have changed, the technology has evolved, so the software created has to conform to these new conditions. Moreover, software projects seem to grow far beyond the hands of a single individual and the formulation of a team working on it appears to be more than necessary [1]. All these factors make the notion of testing and its introduction to the software development process more crucial than ever. There are various approaches to testing and different opinions on the most suitable time to implement it: before the implementation of the actual software, after, or even during the development process. The choice of the approach should be based on the nature of the software project and the needs that each time occur.

Analysis and general comments on the “Software testing: an overlooked process during software development” article

The author introduces the idea of testing and its importance by showing the bad results caused by its absence through a real-life example. The article gives the definition of software testing discriminating its use either for validation or verification of a system. At this point it is quite important to mention the two basics of software testing, which are the black box and white box testing.

  • Black box Testing: It is a technique that does not take into account the internal structure or mechanism of a system. It just focuses on the output generated by the system, given a specific input and specific execution parameters. It is also known as functional testing and is mostly used for validation, since its primary function is to check that the system under testing has the expected behaviour [2].
  • White box Testing: It is a technique that focuses on the internal structure and mechanism of a system. It is also known as structural testing and is mostly used for verification, which means that its primary function is to test that the systems works properly and the way the customer wants it to [2].

The article goes on by demonstrating some of the benefits gained when testing is used. The author then presents a list of different types of tests and analyses unit testing by explaining its use and mentioning the advantages and disadvantages that arise from it. This list could be enhanced with many other types of testing, such as Functional Testing, Performance Testing, Usability Testing, Regression Testing and Beta Testing [2]. The choice of testing method depends on many factors: type and size of software project, available budget, etc. The most important thing that one should bear in mind is that the selection should be done in order to satisfy the needs and requirements that are imposed by a project in the best possible way.

My personal experience with software testing is not that long. In fact, I started learning the basic idea and steps of test-driven development two years ago during my industrial placement, but I began becoming really interested in testing when I was triggered to use it in practice during the “Software Testing” course provided by the University of Edinburgh. Based on the knowledge I have gained so far, I believe that the article I am analysing is generally well-written, has a nice structure and presents the importance of software testing and more specifically the benefits gained by the use of unit testing in a clear and straightforward way. However, I would like to provide more details on unit testing regarding the difficulties encountered during its use and the reasons for which someone should use it or even if it should be chosen over other testing techniques.

Is it really difficult to use unit testing?

The actual process of writing tests for individual units of source code is not difficult, or it may be but at least in a reasonable and acceptable way. The factors that make this process be harder that it really is, are basically two. The first one is related to the nature of unit testing. The goal of this technique is to verify that the requirements that have been imposed by a customer are being met. This is easy to say, but most of the times it is quite hard to identify what the requirements are and which requirements are worthy of testing [3].

The second factor is related to the form of the code and how well it is written and structured. Writing unit tests for “spaghetti” code can be really frustrating. Therefore, if the process of unit testing becomes really hard, that might be an indication that the existing code should be refactored [4]. In general, the code should be divided into smaller fragments that do not have multiple responsibilities. In this way, unit testing can take and test each fragment separately in a very efficient manner.

Why bother writing unit tests?

It is true that the creation of unit tests requires additional effort, time, knowledge, skills. Most of the attention, when evaluation of a system is considered, is based on validation and less effort is put into verification. Unit testing is a standard part of the verification process and that is one of the reasons that make it so important and useful [7].

There are numerous other reasons for which unit testing should be used and embedded in the software development process [6][7]:

  • Unit tests are useful for test-driven development. They are written before the source code of a project. This implies that the code written afterwards should be in compliance with them, making the developer to think carefully about how the code should be designed and ensuring in this way that it will be correct as it is written [7].
  • Unit tests are vital to regression testing. When a new feature is added to an already existing software project, new code is added. In many cases, this can lead to changes that break things that were working perfectly before the new addition and for that reason regression testing is necessary. Unit tests can help in this direction especially when automation of regression testing is required [7].
  • Problems can be identified at an early stage of development. Sources of even very simple mistakes and failures, which most of the time are obvious but for some reason overlooked, can be easily avoided by running some unit tests. The sooner they are discovered and fixed, the better for the rest of the development process [6][7].
  • Unit tests can be used as a kind of documentation. Unit tests are closely related to the design process of a system as a whole and more specifically of the components it is comprised of. Even if there is no formal documentation about a specific part of the system, the unit tests written for that can reveal many information about how it is designed and how it is expected to work [6][7].
  • Unit tests can make integration testing easier: Integration testing checks an entire subsystem and ensures that a set of components work well together [5]. If unit testing has checked the individual components for correctness, then integration testing will perform its job in a much easier and faster way [6].

Should I choose it over other testing techniques?

Some of us may wonder why one should use unit testing when there are other techniques, like integration testing that checks subsystems or functional testing that checks a system as a whole. There are two main reasons that have to do with the performance of the tests and the speed of recovery. Functional tests appear to be slow to run. Therefore, one could use unit tests at compile time as an early sanity-check for the code [5]. Moreover, unit tests are able to point the locations of bugs that have been observed in a better way than functional tests do, making it easier for the developers to act fast and fix them [5].

In general, as far as the testing techniques are concerned, there is no point trying to find the best technique among all the existing. Each testing approach serves different needs and in most cases, a combination of them seems to be the best thing to do. Exploiting the advantages offered by each level of testing can ensure correctness in each fragment of a system as well as in the system as a whole [5].

Finally, to Unit Test or not to Unit Test?

After the above analysis, I think that there is no doubt that software testing is crucial and should be embedded in the development process of software systems. More specifically, unit testing offers many benefits and can save much time and trouble from developers. However, we should always bear in mind that every tool is valuable and can reveal its benefits only when used correctly and wisely.

So my advice is: to Unit test and you will not regret it!

[1] Adam Petersen and Seweryn Habdank-Wojewodzki, “Development Fuel: software testing in the large“, July 2012
[2] Rehman Zafar, “What is software testing? What are the different types of testing?“, March 2012
[3] Marc Clifton, “Advanced Unit Testing, Part I – Overview“, September 2003
[5], “Software Testing: A culture of Quality
[6], “Benefits and Drawbacks of Unit Testing“, September 2012
[7] Chris Cannam, “Unit Testing: Why bother?
[8] SAPM Course Blog, “Software testing: an overlooked process during software development”, February 2014