Forum Moderators: coopster
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