• Advertisement
Sign in to follow this  
  • entries
    25
  • comments
    27
  • views
    14056

Super-lightweight testing for the lazy

Sign in to follow this  

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


0 Comments


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