Create a PHP unit test case using SimpleTest

You can download SimpleTest at

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.)


if (!defined('SIMPLE_TEST')) {
	define('SIMPLE_TEST', 'C:\\simpletest\\');
require_once(SIMPLE_TEST . 'unit_tester.php');
require_once(SIMPLE_TEST . 'reporter.php');


class TestOfLogging extends UnitTestCase {

	function TestOfLogging() {
	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.

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

Try changing the square function and see what happens.