Create a PHP unit test case using SimpleTest

You can download SimpleTest at https://sourceforge.net/projects/simpletest/

Suppose you have a PHP file called math.php that contains functions that you want to test.

<?function square($x) {	return $x * $x;}function cube($x) {	return $x * $x * $x;}?>

Then you can write a test case in another file, say mathtest.php.

(Suppose you have downloaded and extracted SimpleTest to C:\simpletest.)

<?phpif (!defined('SIMPLE_TEST')) {	define('SIMPLE_TEST', 'C:\\simpletest\\');}require_once(SIMPLE_TEST . 'unit_tester.php');require_once(SIMPLE_TEST . 'reporter.php');require_once('math.php');class TestOfLogging extends UnitTestCase {	function TestOfLogging() {		$this->UnitTestCase();	}	function testSquare() {		$this->assertEqual(16, square(4));	}}$test = &new TestOfLogging();$test->run(new TextReporter());?>

(Each function named test* in this class represents a test case and will be run automatically by SimpleTest.)

Run the test case with the command php mathtest.php. As the square function is written correctly, the test case will pass.

testofloggingOKTest cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0

Try changing the square function and see what happens.