Infect me

I’m not test infected yet. I’m almost there. I want to be there. But I’m not there yet.

I just spent an hour and a half fixing a couple of bugs in the online game system I’m writing. It took longer than it should have. Some areas need refactoring a little due to requirements evolution. The main problem, however, was getting to the point where I could test each change that i made took too much time; run server, run clients, do some stuff, watch it fail.

Interestingly I’d viewed having a test harness for this body of code less important than having one for the refactoring project. This code has always been developed with a discipline of fixing all outstanding bugs before writing any new code; writing the simplest thing that could work; and continual refactoring to get clean code that works. This methodology works well. But it’s not enough. Crunch time comes and you don’t have time to muddle through fixing bugs. You want the tests that you never thought you’d need and you want them now. Because you never thought you’d need them having them now is hard. Too hard.

For a while I’ve thought that the important thing is not necessarily always having the tests themselves but being able to add them quickly. If you don’t quite feel you need to practice test Driven development then at least practice testable development. Testable development involves thinking about all of the things that you’d think about if you were doing test driven development but not actually writing that many tests. You’d still have the test harnesses. The harnesses would allow all major code areas to be tested, but your test coverage would be abysmal. Maybe one test per class. Testable development is the first step on the road to test driven development.

Though this may sound a little mad, it actually works quite well in practice. You spend some of the time up front building in the testability but you don’t write tests you don’t need. When things are going well, you move quickly. When things are going badly you can write tests quickly. Today I needed to write tests quickly and couldn’t.

If you’re doing testable development the fact that you write the test harness and plug in all your code to the harness and make sure it runs with your abysmal coverage means that you can test. The classes you develop are testable, they’re loosely coupled, you can plug in mock objects to fake out the services they need. If you’re not doing this then the knowledge of the amount of work that you need to do to go from no testing to a single testable class is enough to make you muddle through in the debugger when you should know better.

I’m too used to working without a net and I thought I was better than I was. You live and learn… Off to create a test harness and write that first test.