Thursday, April 23, 2009

DON'T DO THIS

SERIOUSLY DONT.

When you write unit tests don't ever use loops to check things.  EVER.
As an example:

for (int i=0; i is less than 512; i++)
{
ASSERT(something);
}

Why?  Because when one of the asserts fail, then what happens is that you get the line number of the assert failing.  Which is in the example above, 3.  Which is completely useless.  It won't tell you on which iteration it failed.  And if you are using an ASSERT_FAIL, then it is even more useless.   This is horrible.  This should never be used unless your unit tests test the ability of a generator to test whether or not it can consistently generate good values.  See the example of how to do this that isn't retarded.
A better version to do is:
int numberofFailures=0;

for (int i=0; i is less than 512; i++)
{
if( the same logic of the assert)
else:
numberofFailures++;
}
AssertEquals(numberofFailures, 512);

Another thing:
Don't have output that you have to check manually.  This really is bad.  And I don't mean printing out some number and then checking by hand if that number is the right output.  What I mean is printing out :
-----------------------------------
Test 21: Passed: Good values of tacos.
-----------------------------------
***************************
Test 22: Failed: Too much tacos.
***************************

This is not helpful since you are not using asserts which can then print standardly out to a file which then can be easily parsed and integrated in a report for all the unit tests ran. 

I am not against printing things out, but USE ASSERTS not prints and if's instead.

Also:
If you use a counter to keep track of the current test case being ran, this is stupid.  Use fixed numbers.  It makes things very hard to figure out which test is being ran when everything looks the same!

No comments:

Post a Comment