“I can’t add tests to this app. It’s not got a test suite yet. The code isn’t written in units. I can’t mock out the database”.
These are several concerns I voiced in my head looking at a legacy PHP application. It was a minefield of functions and
include statements. A variable would thread through several files. “I can’t possibly write tests for this” I told my self.
I was wrong. And so are you.
Where to start?
In my case, I started with bugs. When a bug was reported I took the time to add a test then fix the bug. This has a few great benefits
- You’re forced to add some rudimentary testing harnesses
- You prove the bug actually exists
- The bug has already been understood before commencing work
- The bug is documented via a test. Future developers are thus forced to keep it fixed.
- You’ve added the first test to the application!
The guiding principle in any software project is the Boy Scouts rule. In short this rule dictates that the code should always be checked in a little bit better than it was checked out. Not perfection. Just a little better.
The key as with many things is to just get started. Having a test will move the codebase in a good direction.