Unit Testing: Why Bother?

This question has sparked a lot of debates within web developing teams, among programmers and project managers, between those perceiving unit testing as a key software development technique and, well, the skeptical ones. Those considering that unit testing is a waste of priceless time; that writing this kind of tests would automatically add a way too big overhead to the whole development process.Therefore, in this post I'll do my best to advocate for unit testing, to outline its key benefits and how they outweigh the costs resulting from skipping this step. 

But First: What is Unit Testing?

Allow me to try a comparative definition: if testing's main purpose is to help you track down any possible bugs “messing with” your code and, moreover, to help you identify and then develop new functionalities, then unit testing helps you... test (obviously) whether those specific functionalities work well in both the best and the worse possible scenarios.

Have I managed to make myself clear enough or not quite?

Unit testing refers to testing small “bites” of code, individually, meaning independently of their normal application context. And since we're talking about testing fragments of code “in isolation”, in many cases, as you might expect, mocking may be required for a proper unit testing.

In a nutshell: it's the very same developer working on a given code that will create the due tests for each one of his/her units of code. Moreover, the whole unit testing process is, after all, nothing but a good practice that will allow him/her to identify and fix the bugs in his code. As simple as that!

And this is's my list of:

8 Reasons Why Unit Testing DOES Worth the Effort


1. Unit Testing Is Significantly Faster

Just think about it: with over 1000 unit tests that Drupal 8 ships with (targeting its core modules) for you to just run into your code, unit testing turns into a matter of... seconds.

And speed goes hand in hand with “saving priceless time”!

You've already run your tests on key units of your code, you know how these segments of code work in different contexts that you've simulated. Therefore, applying future changes to your code will turn into a blazing fast (and risk-free) process!


2. You Get Constant Feedback As You Create and Test Your Units of Code

And this is gold! 

The on-going feedback that you'll get as you write and test your code will not only improve your overall web development process, but will turn into a great tool for proactively monitoring your code. For anticipating bugs and errors before you even  launch your integration testing process.

Talking about reducing debugging time, right?


3. It Reduces The Debugging Time You'd Spend During Integration Testing

And since I've tackled the issue of “debugging” during integration testing, I felt like I should turn this into a stand-alone benefit: by the time you get to integration testing (when, if bugs “strike”, you realize that anything could be causing it, from a module to a tiny piece of code etc.), you've already tested your code, unit by unit, and eliminated most of the bugs!


4. It Provides You With Test Case Scenarios Acting as Documentation for Your Code

And “case scenarios” do hold much more practical value than an overload of conventional technical documentation. 

Basically, the unit tests created for specific pieces of code will automatically provide you with a clear understanding of what that segment of code (a method or maybe a class) is about and (most of all) of its “role” within the whole code structure. Of how it's supposed to be used!

 

5. Unit Testing Makes It Easier to Refactor Your Code

Unit Testing enables you to restructure your code without impacting its functionality, without a visual change of the system.

Once you have your unit tests written, you're free to run them and thus to ensure that nothing will break once you apply any changes to your code.

Not only that you can stay reassured that your future changes won't affect your code, but unit testing allows you to anticipate the impact of a certain change on anyone of your units of code.

Units that you will have already tested in the best and the worse possible case scenarios.


6. Unit Testing Delivers You Practical Visual Information

And this is one of the “gold” benefits of unit testing: the practical value that these unit tests hold. 

Practically, you'll know, before integration testing, whether a certain button performs the action that it's supposed to perform once clicked on. It allows you to see, beforehand, whether your code behaves as it's supposed to behave (the “practical visual information” that I've mentioned).

 

7. It Enables You to Easily “Joggle With” New Features and Functionality

Scalability is another heavy reason why unit testing is, indeed, worth your time.

Extensively testing your pieces of code enables you to get “daring” and to try new functionalities and new features without fearing that they would break your code. 

Extensive unit testing process will let you know at precisely what point you should roll back. You'll know, by then, which is the precise moment when the new features that you're exploring risk to impact your Drupal website/app's functionality.


8. It Provides You With Flexibility In Applying Changes to Your Code

Flexibility” (and some prefer the word “agility”) is just another word for “unit testing” I could say!

Venturing to apply certain changes to your code, to try new features, won't be as risky once you've already tested every single segment of code and you've already validated its functionality in several possible scenarios.

Moreover, you even get to migrate both your code and your unit tests from one Drupal web project to another. Simply tweak your code till you have your tests up and running again and there you have it: code re-using made simple!

What's your opinion on unit testing? Do you usually incorporate it into your own web development processes or it depends a lot on the specific “development scenario” involved (whether you're working individually on a project as Drupal developer in Toronto  or as part of a team, whether you're writing frameworks or APIs that will subsequently be modified by other coders etc.)?  I “dare” you to spark a new debate around “unit testing” in the comments here below!