by date
Unit Tests for Torque Script
Unit Tests for Torque Script
| Name: | BlueRaja | |
|---|---|---|
| Date Posted: | Jul 20, 2008 | |
| Rating: | 4.5 out of 5 | |
| Public: | YES | |
| Comments: | YES | |
| RSS Feed: | or Subscribe with . | |
| Profile Page: | View profile page for BlueRaja |
Blog post
After learning a bit about TorqueScript, it seemed to me to be a very flexible (albeit limited) scripting language with easy access to metadata - which is why I was so surprised to find out that nobody had written a unit testing framework for it yet.
So yesterday night, I wrote one. It was written for TGB Pro, but will probably work on TGE Pro and may even be edited to work for those without the engine-source.
Please keep in mind that this was my first project using TorqueScript, so I probably did things in a very non-standard (or even incorrect) way - if anyone sees something that should be done a different way, let me know and I'll try to fix it.
To use the testing framework (aptly named TUnit), take the following steps:
1. Before you can use TUnit, you need to edit the engine to allow iteration through an object's methods. To do this, add the following code to simBase.cc and recompile (I placed it on line 1004, but it doesn't really matter):
2. Download the TUnit source code (link to zipped file). Extract the files to your game/gameScripts folder.
3. Add the following line to startGame() in game/gameScripts/game.cs:
That's it! To begin writing tests, simply add a file to the Tests/ directory, and add this line somewhere in the file:
Actual tests take this form:
Please note that All tests must end in the word test.
(For more info on writing tests, see the examples given in the Tests/ directory, as well as the methods available in Assert.cs).
Finally, to run the tests, simply run the game and type
in the console. To run the tests for all classes, type
Here is an example screenshot:

The next thing to do would probably be to write up a mocking framework. If I ever get not-lazy, perhaps I'll do that.
[Edit] I got not lazy.
- BlueRaja
So yesterday night, I wrote one. It was written for TGB Pro, but will probably work on TGE Pro and may even be edited to work for those without the engine-source.
Please keep in mind that this was my first project using TorqueScript, so I probably did things in a very non-standard (or even incorrect) way - if anyone sees something that should be done a different way, let me know and I'll try to fix it.
To use the testing framework (aptly named TUnit), take the following steps:
1. Before you can use TUnit, you need to edit the engine to allow iteration through an object's methods. To do this, add the following code to simBase.cc and recompile (I placed it on line 1004, but it doesn't really matter):
ConsoleMethod(SimObject, getMethod, const char*, 3, 3, "(int index) - Gets the name of the method at the given index")
{
S32 index = dAtoi(argv[2]);
Namespace *nameSpace = object->getNamespace();
Vector<Namespace::Entry *> entryList(__FILE__, __LINE__);
if(nameSpace)
nameSpace->getEntryList(&entryList);
if(index<0 || index>=entryList.size())
return "";
return entryList[index]->mFunctionName;
}
ConsoleMethod(SimObject, getMethodCount, S32, 2, 2, "() - Returns the number of methods callable by this object")
{
Namespace *nameSpace = object->getNamespace();
Vector<Namespace::Entry *> entryList(__FILE__, __LINE__);
if(nameSpace)
nameSpace->getEntryList(&entryList);
return entryList.size();
}
2. Download the TUnit source code (link to zipped file). Extract the files to your game/gameScripts folder.
3. Add the following line to startGame() in game/gameScripts/game.cs:
exec("./TUnit/TUnit.cs");That's it! To begin writing tests, simply add a file to the Tests/ directory, and add this line somewhere in the file:
TUnit::registerClass(myClassName); //Replace myClassName with the name of your testing-class
Actual tests take this form:
function myClassName::testNameTest()
{
//Test stuff here
}
Please note that All tests must end in the word test.
(For more info on writing tests, see the examples given in the Tests/ directory, as well as the methods available in Assert.cs).
Finally, to run the tests, simply run the game and type
TUnit::runTests(myClassName);
TUnit::runAllTests();
Here is an example screenshot:

The next thing to do would probably be to write up a mocking framework. If I ever get not-lazy, perhaps I'll do that.
[Edit] I got not lazy.
- BlueRaja
Recent Blog Posts
| List: | 07/30/08 - Mock-objects for TorqueScript 07/20/08 - Unit Tests for Torque Script |
|---|
Submit your own resources!| Orion Elenzil (Jul 20, 2008 at 20:03 GMT) |
| Ronny Bangsund (Jul 21, 2008 at 01:35 GMT) |
| Gary Preston (Jul 21, 2008 at 11:56 GMT) |
| MrPhil (Philip J. Ludington) (Jul 21, 2008 at 22:57 GMT) |
You must be a member and be logged in to either append comments or rate this resource.


4.5 out of 5


