DEV Community

Andrew Davis
Andrew Davis

Posted on

Why You Should Be Unit Testing

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);
}
Enter fullscreen mode Exit fullscreen mode

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:

Latest comments (10)

Collapse
 
rafalpienkowski profile image
Rafal Pienkowski • Edited

First of all nice article.

Writing unit test is helpful in case of achieving SOLID principles in your code. In my opinion, this is added value of unit tests in our code.

If you want there is a great series of SOLID:

Cheers.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Testing should not prevent anxiety, it should only lessen it.

Unit tests should not be relied upon to catch defects. When refactoring, or writing new code, it's important that you pay as much attention as if you didn't have unit tests. Testing can't make up for bad engineering.

I use extensive unit testing to validate and support my code, but my code can always stand on its own.

Collapse
 
restoreddev profile image
Andrew Davis

I think good tests force you to use better engineering so in that way it makes your code better. Testing won’t catch every defect, but it will help to cut out a lot of them.

Collapse
 
markschweiger15 profile image
Mark Schweiger

Very nice.
Do you write your unit tests before (TDD) or after the code is written?
Also, do you prefer mocking dependencies or integration tests?

Collapse
 
restoreddev profile image
Andrew Davis

I don’t do TDD religiously, but I’m trying to move in that direction. Writing the test before code makes you write your code in a more testable way.

Typically I will mock and do integration. I like mocking when I’m testing a small piece of functionality and I want to mock away another dependency so I don’t have to worry about it. But then I’ll have an integration to make sure it all works together.

Collapse
 
hilaberger92 profile image
Hila Berger

Great article!
I also thought that unit tests are unnecessary at the beginning, but I learned the hard way that it's very necessary and helpful!
Do you use unit tests also to detect bugs?

Collapse
 
restoreddev profile image
Andrew Davis

Thanks! Typically you will write a test every time you fix a bug to verify the bug is fixed and to make sure it doesn't come back again.

Collapse
 
hilaberger92 profile image
Hila Berger

Do you use any unit testing frameworks?

Thread Thread
 
restoreddev profile image
Andrew Davis

I mostly use PHPUnit for PHP and Xunit for Swift though there are a lot of options.

Thread Thread
 
hilaberger92 profile image
Hila Berger

Do you know any frameworks for C#/C++?