Testify.php - a micro unit testing framework

Documentation

Here is a quick, comment free example of Testify:

include "testify/testify.class.php";
include "MyCalc.php";

$tf = new Testify("MyCalc Test Suite");

$tf->beforeEach(function($tf){
	$tf->data->calc = new MyCalc(10);
});

$tf->test("Testing the add() method", function($tf){
	$calc = $tf->data->calc;

	$calc->add(4);
	$tf->assert($calc->result() == 14);

	$calc->add(-5);
	$tf->assertEqual($calc->result(),9);
});

$tf->test("Testing the mul() method", function($tf){
	$calc = $tf->data->calc;

	$calc->mul(1.5);
	$tf->assertEqual($calc->result(),15);

	$calc->mul(-1);
	$tf->assertEqual($calc->result(),-15);
});

$tf->run();

If you like what you see, keep reeding below.

Getting Started

Getting started with Testify.php is really easy. Simply download the archive from github.com, extract the testify folder, and include testify.class.php along with the classes or functions you want to test in a fresh PHP document.

include "testify/testify.class.php";
include "MyCalc.php";

// Create a new test suite
$tf = new Testify("MyCalc Test Suite");

// Our test cases will go here..

// Run the suite
$tf->run();

The test cases, or groups of related tests, are defined by passing a closure to the test() method:

// Adding a new test case to the suite
$tf->test("Testing the Add method", function($tf){
	// Write individual tests here..
});

Noice that the closure takes $tf as an argument. It is important to include it, as otherwise the rest of the examples here will not work. There are other ways to make the $tf object accessible inside the function, but this is more convenient.

include "testify/testify.class.php";
include "MyCalc.php";

// Create a new test suite
$tf = new Testify("MyCalc Test Suite");

// Add a new test case to this suite
$tf->test("Testing the Add method", function($tf){
	$calc = new MyCalc(10);

	$calc->add(4);
	$tf->assertEqual($calc->result(),14);

	$calc->add(-5);
	$tf->assertEqual($calc->result(),9);
});

// Run the test and generate a report
$tf->run();

Congratulations! You wrote your first test. See it in action here.

Method Reference

These methods (and properties) help you organize your tests and group them logically into test cases.

data

This is a public property, instance of stdClass. You can use it to share objects and variables between tests. Example:

$tf->data->calc = new MyCalc(10);

// It is now available in your test cases:
$tf->test("Example",function($tf){
	$tf->data->calc->add(42);
});

test( $title, $callback )

The test function defines a test case. It takes a title and a callback function (closure). This function is stored internally and executed when we call the run() method. The title is optional, but recommended.

$tf->test("This is the test case title", function($tf){
	$tf->assert(true);
	// More tests go here..
});

run( )

This method is called once after all your test cases have been defined. It executes the stored test cases and prints the result page.

$tf->run();

before( $callback )

This function is executed once, before any of the test cases. You can use it to instantiate objects or prepare the environment for your tests.

$tf->before(function($tf){
	// Calc will be available in all
	// subsequent method calls
	$tf->data->calc = new MyCalc(10);
});

beforeEach( $callback )

This function is executed once, before every one of the test cases. You can use it to instantiate objects that need to start fresh for every test case.

$tf->beforeEach(function($tf){
	// This will recreate the calc object
	// for every test case
	$tf->data->calc = new MyCalc(10);
});

after( $callback )

This function is executed once, after all the test cases are completed. You can use it to clean up, delete temporary files or close DB connections.

$tf->after(function($tf){
	// All the tests have completed
	unlink("tmp.txt");
	$tf->data->DB->close();
});

afterEach( $callback )

This function is executed once for every test case, after it is complete. You can use it to clean up or gather stats after each test case.

$tf->afterEach(function($tf){
	// After a test has completed
	unlink("tmp.txt");
	$tf->data->DB->close();
});

Test Methods

You can use these methods inside a test case to test for different conditions.

assert( $cond )

This is the basic test method. $cond is a boolean expression. The test is considered pased if $cond is truthful. You can emulate all of the other test methods using this one.

// Inside a test case
$tf->assert( true ); // pass
$tf->assert( false ); // fail

assertFalse( $cond )

The opposite of assert().

// Inside a test case
$tf->assertFalse( true ); // fail
$tf->assertFalse( false ); // pass

assertEqual( $exp1, $exp2 )

This method compares $exp1 to $exp2 using PHP's non-strict comparison operator (==).

// Inside a test case
$tf->assertEqual( 1, 1 ); // pass
$tf->assertEqual( 1, '1' ); // pass
$tf->assertEqual( 1, 2 ); // fail

assertIdentical( $exp1, $exp2 )

This method compares $exp1 to $exp2 using PHP's strict comparison operator (===).

// Inside a test case
$tf->assertIdentical( 1, 1 ); // pass
$tf->assertIdentical( 1, '1' ); // fail
$tf->assertIdentical( 1, 2 ); // fail

assertInArray( $needle, Array $heystack )

This method checks whether variable $needle is a member of array $heystack. It uses PHP's in_array() function internally.

// Inside a test case
$tf->assertInArray( 1, array(1,2,3,4,5) ); // pass
$tf->assertInArray( 1, array(3,4,6,7) ); // fail

assertNotInArray( $needle, Array $heystack )

The opposite of assertInArray()

// Inside a test case
$tf->assertNotInArray( 1, array(1,2,3,4,5) ); // fail
$tf->assertNotInArray( 1, array(3,4,6,7) ); // pass

pass( )

Unconditional pass.

// Inside a test case
$tf->pass(); // pass

fail( )

Unconditional fail.

// Inside a test case
$tf->fail(); // fail