When I started programming I didn’t write any tests. Which was fine because most of the developers I worked with didn’t write tests either. It just wasn’t part of our programming culture.
Eventually someone had to test the code, though, and most of the time that “someone” was the end user. Sure, we did some manual testing before we released an update, but we couldn’t test all the different scenarios. That would have taken way too long. So, we released sorta-tested code and the users let us know when that code was buggy. Once they found the errors we had overlooked, we would write a fix for the bug, manually verify that it seemed to work, then deploy it to production. Sometimes we got lucky and the bug went away. Sometimes we made things worse.
Gradually I started writing tests because I kept hearing it was the “right thing to do”. I didn’t know why I was writing them, except to seem more professional. And honestly, my tests weren’t very helpful. But after a while I had a working test framework and started to understand how to write tests. I was proud to brag that I wrote tests for my code, but I agreed with my non-testing peers that it was mostly a lot of extra code for little gain.
Then I decided to try writing my tests while I wrote the code. I was working on a feature with fearsomely complicated business logic and struggling to wrap my mind around what needed to happen. But the act of writing out the test scenarios changed my whole outlook on testing. That gnarly bit of code that I couldn’t figure out suddenly became manageable because I didn’t have to hold all the logic in my head. With the various scenarios written as tests I could see all the different routes through the code and think clearly about how I needed to handle them. And with the tests, when I modified the code to handle one issue I could know instantly if I had accidentally broken another scenario with my changes.
That’s when I realized there is more to writing tests than having “code coverage” to brag about. The tests actually help you write good code. Written correctly, the tests actually save you a lot of time.
Since that day I’ve been writing tests regularly and have been reminded repeatedly of their value. The act of writing the test has made me think about scenarios I would have overlooked, and the tests themselves have caught bugs long before they became an issue.
Tests make me a better programmer and provide a more reliable experience for my users, because now I find the bugs before they do.
This is a two-part slide deck. The first 32 slides were presented as a motivational talk at Sunshine PHP in 2020.
The whole deck (all 38 slides) was presented as a workshop for my colleagues at Vanderbilt University Medical Center.