The first time I discovered unit tests was when I was working with a Python developer in the late 2000s. I can’t remember how it came about, but it was through him that I learned about the concept, how you write the tests first and then write the code to pass the tests. I still didn’t understand the value, so I did some cursory research but generally moved on.
Over the course of the next few years I often saw articles or discussions on unit testing, but the idea of unit testing my code was not something that was a part of any developer position I’ve held in the last 15 years, so I never learned how to, or why I should, write unit tests.
In 2015, when I started developing for WordPress, unit testing came up again, as I looked into contributing to WordPress core. In my search to ramp up my WordPress development knowledge I discovered Know the Code, and one of Tonya’s courses was about unit tests. At around the same time I started using Laravel, which meant eventually finding Laracasts, which also included a course on Unit Testing. Through these places I eventually discovered Grumpy Learning by Chris Hartjes, who has 3 books dedicated to unit testing PHP code.
I’ve since come to appreciate the value and need for unit tests and have committed myself to writing unit tests for all new functionality I code from 2019 onward. At first it was daunting, but today something finally clicked.
It started with a new feature. I needed to verify the extension of a media file path, ignore any query strings that might be appended to that path, and return the correct base name to the actual file. Contrary to my unit testing resolution, I wrote the actual code first, but realised that when it came time to test it, I’d need to do a whole bunch of other work to deploy the code and test it manually, the old fashioned way. I realised this was a great chance to write some unit tests.
During the writing of some simple assertEquals() assertions, I soon realised that my initial understanding of the problem was flawed. By writing a few additional tests for cases I had not originally thought of, I could more thoroughly test my solution and improve it to handle these new situations.
In a round about way, I ended up eventually writing the correct unit tests I should have written in the first place, rewriting my code from scratch to solve these tests, and ending up with a much better overall solution.
The tests themselves and the code solution was trivial. What was important was the realisation that, had I started writing the tests first, my mind would have provided additional cases I might not have thought of. I would therefore be preparing myself to not only come up with a better solution, but with a much faster way to test and confirm it.
Through all this I’m getting a good grasp of which types of problems lend themselves to unit testing and which do not. I also realise that if I’m going to write more unit tests I need to allow myself more time up front to plan and execute proper unit testing. I’m still learning, so things like mocks and stubs are still far off concepts I’m aware of but will need to master. I am however excited to see how this improves as I practice, and how it improves my development output as a whole.