• entries
25
27
• views
14094

# Super-lightweight testing for the lazy

299 views

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 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 =)

There are no comments to display.

## Create an account

Register a new account