Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

Test Driven Development

Sign in to follow this  
Stephen R


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:

  1. Write a new test.

  2. Run tests and confirm that the new test fails.

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

  4. Run tests and confirm that all tests pass.

  5. Refactor/clean code.

  6. 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:
void CanCreateNumberWithValue()
NumberClass n(1);

I run this test, and it fails, it doesn't compile, so first let's make it compile so we can ensure the test fails.

class NumberClass {
NumberClass(int n) { }
int GetInt() { return 0; }

Running the tests, it is clear that this will fail. To only just get the test to pass, let's do the following:

class NumberClass {
NumberClass(int n) { }
int GetInt() { return 1; }

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.

So now we add a second test, for another value.

void CanCreateNumberWithValue2()
NumberClass n(2);

Obviously, running the tests will result in a pass for test 1, and a fail for test 2. So now we want to make the code pass test 1 and test 2. So now we change the class to this:

class NumberClass {
int value;
NumberClass(int n) { value = n; }
int GetInt() { return value; }

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)
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!