TLUnitTest Class Reference

Very simple unit tester. More...

List of all members.

Public Member Functions

 __construct ($appName, $testClass)
 Create a new Unit test controller.
 __destruct ()
 errorHandler ($errno, $errmsg, $filename, $linenum, $vars)
 The custom error handler. Don't use this.
 exceptionHandler ($e)
 The custom exception handler. Don't use this.
 passed ()
 Mark a single test as having passed.
 failed ($e)
 Mark a single test as having failed. It will mark the test as such and generate a (useable) backtrace.
 assert ($bool)
 Assert a boolean expression. If it returns 'true', the test will be marked as passed. Otherwise it will be marked as failed.
 dumpHtml ($hidePassed=false, $sortGroups=false)
 Generate a beautiful dump of the test results in HTML format. This outputs a single HTML page.
 dumpPrettyText ($hidePassed=false)
 Generate a beautiful dump of the test results in Text format.


Detailed Description

Very simple unit tester.

This class can be used as a very simple Unit tester. It simply runs all methods on a class (both static and non-static) and catches all thrown exceptions and PHP errors. Alternatively, you can call $test->assert(ASSERTION); from the methods in the class to test for things.

If the test class you pass to the constructor contains a property named 'testNames', it will be used to display pretty names for the methods (tests) in the class. testNames should be public, should be an associative array where the keys match the methodnames and should look like this: public $testNames = array("GroupName_TestName" => "test the foobar", ...);

For each called method in the class to be tested, the first parameter passed to the method will be the Tester class. You can use this to assert things from your test case like this: public function MyTestCase($test) { $test->assert(a != b); }

You can subdivide test methods in groups by putting underscores in the method names. Methods are called in the order as they appear in the test class but may be sorted by group in the final results.

An example test class could look like this:

 class TestMe {
   public $testNames = array(
     "User_Load" => "Load a user",
     "User_Save" => "Save a user",
     "Grant_User" => "Give rights to a user",
     "Grant_Group" => "Give rights to a group",
   );
  
   public function User_Load($test) {
     $this->user = new MyProgramUser("john");
     $test->assert($this->user->getUserId() == "john");
   }
   public function User_Save($test) {
     $this->user->save();
   }
   public function Grant_User($test) {
     $this->user->grantPrivilge(READ);
   }
   public function Grant_Group($test) {
     $this->user->group->grantPrivilge(READ);
   }
   public function User_Load_NonExisting($test) {
     // Test PASSES if an error is thrown! (it's supposed to do so)
     $test->failed(new Exception("Non existing user loaded."));
     try {
       new MyProgramUser("AmeliaEarhart");
     catch (Exception $e) {
       $test->passed();
     }
   }
 }

Example output could look like this:

  Nr | Test                        | Passed | Result
 ----+-----------------------------+--------+-----------------------------------------
   1 | User:Load a user            | passed | 
   2 | User:Save a user            | passed | 
   3 | User:Load_NonExisting       | FAILED | Non existing user loaded.
   4 | Grant:Give rights to a user | passed |
   5 | Grant:Give rights to a group| passed |
   6 | Group:Add user to group     | passed | 
 

Reports can be generated from the results using the ->dumpFoo() methods.

Warning:
Please be advised that this class does strange things to error reporting and handling. Do not be surprised if errors suddenly don't turn up anymore after you've created an instance of this class.

Definition at line 356 of file tlib.php.


Constructor & Destructor Documentation

TLUnitTest::__construct ( appName,
testClass 
)

Create a new Unit test controller.

Parameters:
$appName (string) The name of the application you're testing.
$testClass (string or object instance) An instance of or name (when only using static methods) of a class to test.
Warning:
Make sure to destroy this class when you're done unit testing, or errors will not show up anymore.

Definition at line 372 of file tlib.php.


Member Function Documentation

TLUnitTest::assert ( bool  ) 

Assert a boolean expression. If it returns 'true', the test will be marked as passed. Otherwise it will be marked as failed.

Parameters:
$bool (Boolean) Boolean result of an expression.

Definition at line 506 of file tlib.php.

References passed().

TLUnitTest::dumpHtml ( hidePassed = false,
sortGroups = false 
)

Generate a beautiful dump of the test results in HTML format. This outputs a single HTML page.

Parameters:
$hidePassed (Boolean) If true, passed tests will not be shown. This is useful when you've got alot of testcases and only want the failed ones to show up.
$sortGroups (Boolean) If true, the results will be sorted by group. Otherwise the results will be listed in the same order they where executed.

Definition at line 534 of file tlib.php.

TLUnitTest::dumpPrettyText ( hidePassed = false  ) 

Generate a beautiful dump of the test results in Text format.

Parameters:
$hidePassed (Boolean) If true, passed tests will not be shown. This is useful when you've got alot of testcases and only want the failed ones to show up.

Definition at line 610 of file tlib.php.

TLUnitTest::errorHandler ( errno,
errmsg,
filename,
linenum,
vars 
)

The custom error handler. Don't use this.

Note:
DO NOT USE THIS, ITS FOR INTERNAL USE ONLY.

Definition at line 392 of file tlib.php.

References failed().

TLUnitTest::exceptionHandler ( e  ) 

The custom exception handler. Don't use this.

Note:
DO NOT USE THIS, ITS FOR INTERNAL USE ONLY.

Definition at line 406 of file tlib.php.

References failed().

TLUnitTest::failed ( e  ) 

Mark a single test as having failed. It will mark the test as such and generate a (useable) backtrace.

Parameters:
$e (Exception) The exception that occured.
Note:
This is also called by the custom error handler, which mimics a thrown exception

You can safely call this method to set the default pass/fail status of a test method and later on in the test mark it as having passed.

Definition at line 468 of file tlib.php.

Referenced by errorHandler(), and exceptionHandler().


The documentation for this class was generated from the following file:
Generated on Sun Sep 24 22:05:25 2006 for TLib-PHP by  doxygen 1.4.7