🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Super-lightweight testing for the lazy

posted in MyJournal.lnk
Published September 19, 2005
Advertisement
Today I've been working on a texture manager as part of my library of useful components which I'll hopefully use on my next project. Now I don't want to delve into how it works, because that's not what I want to blather on about.. what I *do* want to talk about is test-first development.

Step 1. Draft up the header file with the class interface.
Step 2. Put method bodies for Getters in the header(eg, GetNumLoadedTextures()).
Step 3. Create simple method bodies for more complex functions inside the source file.
Step 4. Return either false, 0, or other fail case for each method where appropriate.
Step 5. Construct tests in the main function.

Step 6...
Haang on, do I still have cppunit? Nope. Do I actually need it? Nope. I decided it was a bit draconian for what I need, and I could probably knock up something in about 5minutes which would achieve the same end result:

static int assertFailed = 0;static int assertPassed = 0;void zassert(bool cmp,const std::string& msg){ if (!cmp) {  std::cout << "FAILED:" << msg << "\n";  assertFailed++; } else {  std::cout << "PASSED:" << msg << "\n";  assertPassed++; }}void zstatus(){ std::cout << "Tests executed:" << (assertFailed+assertPassed) << "\n"; std::cout << "Failed:" << assertFailed << "\n"; std::cout << "Passed:" << assertPassed << "\n"; std::cout << "Overall Result:.... \n\n"; if (assertFailed > 0)  std::cout << "       -- FAILED --"; else  std::cout << "       -- PASSED --"; std::cout << "\n\n\n"; assertFailed = 0; assertPassed = 0;}


Now of course it isn't reusable in it's current form(other than copy+paste), and the majority of people will want to wrap the functionality up in a namespace or Singleton, add optional file output, emailing of results, etcetc, but really it's all you need to get your drafted test cases into code so you can begin bug fixing.

If you want to modularize your tests, you could just call zstatus() after each block, as the counters are reset, eg:

void testWeaponSystem(){ std::cout << "Testing weapon systems...\n"; ... zstatus();}void testShields(){ std::cout << "Testing shields...\n"; ... zstatus();}int main(...){ testWeaponSystem(); testShields();}


Nice and easy =)
Previous Entry Network Stuff
Next Entry Long time no siege
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement