Test Driven Development

Long time since I've posted. The short version of things is I'm still in college, soon to be finished, and want to do something about getting a job in games when that happens.

To that end I'm starting making a game. The design is firming up, beating it out at the moment. Technology wise DX9,C++,Boost,FMOD are some of my choices so far, but that's not particularly interesting.

What's interesting, and what I want to write about here, is my decision to try do this via Test Driven Development. This summer I've been working in Microsoft, and I've been made develop using this TDD method, which I'd never heard of before. Initially I thought it was just unit testing, going under some new buzzword. But it's not at all. It's so much more than that.

A brief summary of the TDD process:

Write a new test.

Run tests and confirm that the new test fails.

Write *ONLY JUST ENOUGH* code to make the test pass.

Run tests and confirm that all tests pass.

Refactor/clean code.

Rerun tests and confirm that they all pass.

To give a bit of an example lets say I needed to develop an Object oriented encapsulation for integers. My first test might be:

And now running the tests this passes. Only enough code has been added to pass the test. This is a very important thing. There is no code that there hasn't been a test written for. Because the tests are a complete description of the features of the program, the tests will ensure that any and all implicit contracts are preserved. While this is a trivial example, I have found that rigorously doing this has caught bugs before they happen.

And now test 2 passes as well, and test 1 will still be passing. It's worth noting that before we added test 2, the class was fully compliant with the specifications for what the class should do, with that set of tests.

You might think you'll end up wasting a lot of time with this. But what I found was that I ended up spending so little time agonising over class design decisions. Bugs became incredibly rare. I was much happier with the rate at which final code was being produced.

TDD is an amazing design methodology, with testing built right into it. The thing I had to realise, and it came out of my thinking about whether I should be testing private methods, is that in step 1 of the process you aren't writing a unit test. You're instead specifying a new feature for the program/library. Whether you need to implement that in terms of other existing functions which can lead to refactoring common functionality to private members, or whether it's all new functionality that requires new classes it doesn't matter.

One side-effect of this is, for me anyway, is that my encapsulation is much better. My classes are better designed. Clean interfaces are defined.

And it is this last point that is concerning me at the moment. There are things that can't be tested properly, like creating a window and setting a resolution and a pixel format. Or rendering a model to the screen. These sort of things you can't test like this. Instead you need to abstract away from them. For an example of this I might have a GraphicsKernel class that I'm testing these functionalities with. But to be able to test them, GraphicsKernel has to take an interface to a low level implementation. And the testing methods need to be able to instantiate the GraphicsKernel using a replacement test object, which implements the interface, without the system dependancies.

I think the effort of creating these abstractions should be worth the reward of having a comprehensive

Feeback Request: If this is any interest to people I can write up the process of getting native C++ code tested using the Visual Studio testing framework. Getting it working with managed code is pretty trivial.

(Accidentally posted this in General Programming and not my journal :P)