A very simple way to do unit testing in PHP. Part of my Code Grimoire.

<?php
// tests.php -- Trivial Unit Testing
// 2007-12-17 Felix Pleşoianu <felixp7@yahoo.com>
// If you are asking what license this software is released under,
// you are asking the wrong question.

function init_tests() {
	global $_success_count, $_failure_count, $_failed_tests;

	$_success_count = $_failure_count = 0;
	$_failed_tests = array();
}

function test($code) {
	global $_success_count, $_failure_count;

	if (assert($code)) {
		$_success_count++;
	} else {
		$_failure_count++;
	}
}

function handle_failed_test($file, $line, $code) {
	global $_failed_tests;

	$_failed_tests[] = $code;
}

function report() {
	global $_success_count, $_failure_count, $_failed_tests;

	printf("%d tests ran, %d succeeded, %d failed\n",
		$_success_count+$_failure_count,
		$_success_count, $_failure_count);
	if (count($_failed_tests) > 0) {
		print "Failed tests:\n";
		foreach ($_failed_tests as $code) {
			print "\t$code\n";
		}
	}
}

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'handle_failed_test');

header('Content-type: text/plain');

init_tests();

// Put your own tests below this line.
test('true;');
test('false;');
test('mysql_query("");');
// Put your own tests above this line.

report();
?>
Last modified: Tue 04 05 2010, 12:45:12 UTC