Test Code in game script

Your C/C++ code is asynchronous code, so it’s difficult to use a unit test framework. Game objects (actors) consist of some assets that are loaded async, receive message from asynchronous message system and have many behaviours that you should check with your eyes.

Where should you write test code for game objects? One of solutions is that you write test code in game script. It’s possible to wait the timing that asynchronous process completes, because game script can do yield/resume in the game main loop.

For example, to test whether a game object is able to equip a certain weapon, you may write the following setUp function in script.

  function setUp() {
    mObj = Entity.Load("soldier");
    mArm = Entity.Load("sword");

    while (!mObj.hasLoaded()) yield();
    while (!mArm.hasLoaded()) yield();
  }

After setUp(), you can write test code and tearDown().

  function testEquipSword() {
    TS_ASSERT(mObj.equip(mArm));
  }

  function tearDown() {
    mObj.unload();
  }

Maybe, for core system engineers, this is a kind of integration test. However, for an engineer writing game specific code, this is really unit test. Writing test suite and test case in script detects issues as well as standard test unit written in C/C++.

 

When you write test case in script, you need good assertion. Most script language don’t provide macro feature, but assert() function is there, so you may use assert right now. In many cases, assert() is implemented as a builtin function in default librariy. If assert() is not enough for your test, you can change source code of assert().

Many game specific languages aren’t rich than generl script languages — Python, Ruby, Perl and PHP. Perhaps you write table instead of class. But, implementation of xUnit family in those languages is good reference for us. I used example from “PythonUnit” and wrote simple unit test framework.

– Add TestCase instances to TestSuite.
– Call TestCase.runTest() after TestCase.setUp().
– Call TestCase.tearDown() after test is failed.
– textXXX() methods are called automatically, so you don’t need to implement runTest() in most case.

Assert function that is provided in default may be unenough to stop test when a test in test cases is failed. So you use example from the assert() function, write custom assert() function and bind it as new assert() builtin function.

Advertisements
This entry was posted in Game Programing and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s