Forum Moderators: coopster

Message Too Old, No Replies

Fast Unit Test Method

         

cybersphere

12:11 am on Jul 13, 2006 (gmt 0)

10+ Year Member



I've come up with a new method for unit testing classes.

Initially, I was placing my test harness code at the start of the class, which was messy, since I would be commenting and uncommenting blocks of test code, while testing.

I wanted to head towards a more formal approach, but using a proper unit test framework carries more overhead than I want to deal with at present.

In order to come up with a simple go-between, I added the following 2 methods to my class:

function testall() {

$counter=1;
echo "Starting Tests<br>";

while([classname]::test($counter))
$counter++;
echo "Ending Tests<br>";
}

function test($test_to_run) {
echo "<br>RUNNING TEST $test_to_run: ";
$nomatch=false;
$classinstance=new [classname]();

switch($test_to_run) {
case 1:
echo "test description goes here";
// test implementation code goes here
break;
case 2:
echo "test description 2 goes here";
// test implementation goes here
break;
default:
$nomatch=true;
}

if ($nomatch) return false; else return true;

}

You can use this code to run a single test, or run all tests.

For example,

[classname]::testall(); // runs all tests
[classname]::test($testnumber); // runs a specific test

coopster

5:02 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There are also some PEAR packages [pear.php.net] available in the Tools and Utilities section if you are interested.