Why You Should Be Unit Testing

Feb. 19, 2018 • 5m 12s

Unit testing feels like running for exercise. It is something you know you should be doing, but it is really hard to get motivated to start. When I first began programming, I thought unit testing was redundant. I manually test everything, so why should I take the time to write more code? It took time for me to learn unit testing, but now I see the benefits. Here are a few reasons why unit testing is so important.

Testing Keeps Your Code Organized

I work mostly with scripting languages (PHP and JS) and when you use scripting languages, it is really easy to put all your functionality in one place. An example would be writing a function that imports a CSV file. It is tempting to put the file opening logic, data validation and database inserting in one function. However, doing so makes the function long and hard to understand.

What happens when you try to unit test a long function like that? In unit testing, you want your code to be in smaller chunks so that it is easier to create a test case for it. You might break up your import function to have three different functions: one for finding and opening the CSV file, one for validating the data and one for running the SQL queries to insert the data into the database. Now, it is easier to test. As a bonus side effect, your code is organized into smaller functions making it cleaner and easier to maintain.

Testing Is Code Documentation

When you write a test case, it acts as documentation for how to use the function you are testing since you get to see it in action. Let’s say that you have a function that validates a variable is a number and you write a test case for it:

public function test_run_validate_number_with_real_number()
{
    $value = 5;

    $result = validateNumber($value);

    $this->assertTrue($result);
}

What do we know about this validateNumber function now that we have seen the test? First, we know the function’s name and how many parameters it accepts. Second, we know that it responds with true if the value passed is a real number. All without having to view the function itself!

This is a small example, but it illustrates how tests can explain your code. If you have a test suite, you can use it to train other programmers on how to use your code.

Testing Prevents Anxiety

Have you ever been afraid to change your code because something will break? Do you get nervous before pushing to production? Do you dread telling a client that you messed up? I do! The anxiety makes programming harder and more stressful.

“Imagine being able to make any change you want in your code and know that you did not break something.” ~ Sandi Metz

I heard Sandi Metz say this quote at Laracon a couple years ago and it really changed the way I think about testing. She advocates testing as a safety net for your programming. I now really covet the protection of a test suite. My goal is to have unit tests that verify all the main functionality of an app. It doesn’t prevent all bugs, but it does verify that the core of my app will always work. I can push changes with confidence. No more worrying about angry calls and emails!

Learning how to unit test is difficult, but very rewarding. It will take your programming skills to the next level and give you even more confidence and pride in the code that you write. It cuts down on the stress of change, giving you the ability to deliver better features with more stability. Now, go write some tests!

Here is a list of resources that helped me learn testing: