Probably the single biggest regret that I have in my development career thus far is that I didn’t start writing unit tests for my code at a earlier stage. I was kind of a late adopter to testing and only really started testing my code properly with unit and integration tests in the last 4 years.

If I had started writing tests earlier, I know for sure that I would not only have saved myself a ton of time, but also have become a better developer faster. That’s because testing has changed the way that I write code, for the better.

The biggest argument that is used against testing is that it takes too much time to write tests. And it’s hard to argue against the fact that is does add overhead to your development. But the amount of time that you’ll save in the long run by having a decent test suite far outweighs the initial cost of writing the tests.

It’s hard to convince developers who have never actually felt the real benefit of a good test suite of the long term benefits of testing. If you’ve never written tests before or worked with a codebase that is tested, then bear with me as I try my best to explain why you should unit test your own WordPress plugins.

3 reasons why you should unit test WordPress plugins

There are 3 reasons why I continue to write tests for my code.

Unit tests eliminate existing and future bugs

By writing unit tests, you are both testing your code in it’s current state and making sure that it does what it is supposed to do. And you’re also writing a test that can be used over and over again to make sure that your code is still doing what it is supposed to do as you add more functionality.

I recently took a piece of code in a WordPress plugin that was not tested, wrote a test for it and discovered that it was actually not doing what it was supposed to. If that code got shipped in it’s pre-test ready state, it would have probably resulted in support tickets and frustrated customers. Writing tests can be a great way to debug code that doesn’t already have tests.

Unit tests document your code

Before I look at a piece of code, I like to check if there are tests and if so, read through them as they usually tell you exactly what the code is doing and shouldn’t be doing.

By writing tests, you’re providing documentation for other developers who may work on the same codebase. Never mind other developers, have you ever gone back to a piece of code that you’ve written and been confused as to what is going on? I have, often.

Good tests are usually so much more effective at explaining the ins and outs of your code than badly written documentation or incomplete code comments.

Unit tests make refactoring code much easier

How many times have you refactored code, only to realise later on that your refactor broke some related functionality in your plugin? Perhaps the code that you refactored was not just written for the feature you were refactoring, but for other features too and you didn’t realise it. This happens often if you’re not writing tests.

With decent test coverage, you’ll know exactly if a piece of code you’ve written has broken some other part of your code because the tests will fail.

Are these good enough reasons to start unit testing your plugin?

I think so. And that is because the 3 reasons I listed above will do two things for you:

  1. Save you time in the long run. You’ll write more bug free code which will reduce support requests and debugging headaches. You will also not have to explain over and over again how your code works both to yourself and others.
  2. Make you a better developer. You will be able to push code with confidence knowing that you’re not breaking your plugin. You will also find that writing unit tests forces you to use good programming patterns and design.

If you’re still not convinced that tests are beneficial, all you need to do is work on a codebase which has decent test coverage and feel it for yourself. I know that I am a better developer for it.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.