Back to Posts

The Importance of Unit Tests

Recently I've taken on a new job as a software engineer. As part of this job, I'm required to write unit tests for all my work. Up until this point in my software career, I haven't built tests for the code I've written. Personal projects or frontend websites have never truly warranted them. At first, I was frustrated that I had to spend extra work on writing code for my code, where was the fun in that? It wasn't until I went back to my own personal projects with a determination to write unit tests for all of them and get to 80% code coverage or better that I finally realized how important unit tests really are - they saved my code.

Example

Here is an example we can look at of a recent unit test I was writing and why it's important:

# My code logic
message = 'example error message'
raise ValueError(message)
LOGGER.critical(message)

# My test
...
message = 'example error message'
with pytest.raises(ValueError) as error:
    my_module.my_function()
assert mock_logger.critical.called_with(message)
assert message == str(error.value)

This test fails - it took me a couple hours to find out why after playing with all sorts of ways to mock logging but that's beside the point. Did you spot why it failed? The logger never gets called because an error is raised first. This code has sat this way for months meaning that until I came back to this project and wrote unit tests for it, I wasn't aware of this bug in the code! As soon as I swapped the logger to come before throwing the error, the test passed which also means I fixed a longstanding bug. This is a small and seemingly trivial example but this same thing occurred over and over as I wrote tests for each of my projects.

Lessons Learned

Writing tests for my projects opened my eyes to MANY things I needed to be doing better. Here are just a few:

  1. Unit tests expose bugs that are either overlooked or may not always manifest themselves in normal end-to-end testing
  2. Unit tests expose unused code as you try to build test coverage - I found that much of my code could either be removed or reworked once I realized that I didn't need to test code that wasn't ever being used and therefore it could be deleted
  3. Unit tests expose functions that do not return anything at all or anything meaningful - I've since made sure that all my functions return something
  4. Unit tests guide you to write small units of testable code - much of my code in its first iteration wasn't testable because functions either did way too much (separation of concerns, limit scope, single assertion). Writing unit tests helped me refactor much of my code to be easier to read and maintain
  5. Unit test coverage reports push you to write better code because it ensures you can actually test everything. This in turn improves user confidence in your code as they know it's been tested - it can even be addicting to improve your test coverage on a project

I'd encourage anyone whether you are just starting your software career or are deep into it to write tests for your code. It exposes bugs, helps you write better software, and is incredibly insightful into what needs to change. Not to mention, it's much easier to do from the beginning than it is to come back to.


Justin Hammond
I am passionate about new, innovative technologies. Programming video games since the age of 12, websites at the age of 14, and starting Tuneup Technology (an IT consulting and repair business) at 16, I have years of experience in technology. I co-founded NCR4 (a web design and hosting company) and played a major role in the creation of Nozani (an ecommerce ageny) in my early 20's. I have been involved with multiple startups in both leadership and technology capacities. Excited about the future, I am always on the lookout for the next big thing!


Comments

Please login to leave a comment.

No comments yet.