![]() |
![]() |
![]() |
Simple Test PHP Unit Test Framework | Simple Test PHP Unit Test Framework | Group tests |
The core system is a regression testing framework built around test cases. A sample test case looks like this...
For example...
Our only test method at the moment is testCreation() where we check that a file has been created by our Writer object. We could have put the unlink() code into this method as well, but by placing it in setUp() and tearDown() we can use it with other test methods that we add.
The setUp() method is run just before each and every test method. tearDown() is run just after each and every test method.
You can place some test case set up into the constructor to be run once for all the methods in the test case, but you risk test inteference that way. This way is slightly slower, but it is safer. Note that if you come from a JUnit background this will not be the behaviour you are used to. JUnit surprisingly reinstantiates the test case for each test method to prevent such interference. SimpleTest requires the end user to use setUp(), but supplies additional hooks for library writers.
The means of reporting test results (see below) are by a visiting display class that is notified by various assert...() methods. Here is the full list for the UnitTestCase class, the default for SimpleTest...
assertTrue($x) | Fail if $x is false |
assertFalse($x) | Fail if $x is true |
assertNull($x) | Fail if $x is set |
assertNotNull($x) | Fail if $x not set |
assertIsA($x, $t) | Fail if $x is not the class or type $t |
assertNotA($x, $t) | Fail if $x is of the class or type $t |
assertEqual($x, $y) | Fail if $x == $y is false |
assertNotEqual($x, $y) | Fail if $x == $y is true |
assertWithinMargin($x, $y, $m) | Fail if abs($x - $y) < $m is false |
assertOutsideMargin($x, $y, $m) | Fail if abs($x - $y) < $m is true |
assertIdentical($x, $y) | Fail if $x == $y is false or a type mismatch |
assertNotIdentical($x, $y) | Fail if $x == $y is true and types match |
assertReference($x, $y) | Fail unless $x and $y are the same variable |
assertClone($x, $y) | Fail unless $x and $y are identical copies |
assertPattern($p, $x) | Fail unless the regex $p matches $x |
assertNoPattern($p, $x) | Fail if the regex $p matches $x |
expectError($x) | Swallows any upcoming matching error |
assert($e) | Fail on failed expectation.html object $e |
Some examples...
Note that SimpleTest cannot catch compile time PHP errors.
The test cases also have some convenience methods for debugging code or extending the suite...
setUp() | Runs this before each test method |
tearDown() | Runs this after each test method |
pass() | Sends a test pass |
fail() | Sends a test failure |
error() | Sends an exception event |
signal($type, $payload) | Sends a user defined message to the test reporter |
dump($var) | Does a formatted print_r() for quick and dirty debugging |
Of course additional test methods can be added to create specific types of test case, so as to extend framework...
To prevent this test case being run accidently, it is advisable to mark it as abstract.
Alternatively you could add a SimpleTestOptions::ignore('FileTester'); directive in your code.
This new case can be now be inherited just like a normal test case...
If you want a test case that does not have all of the UnitTestCase assertions, only your own and a few basics, you need to extend the SimpleTestCase class instead. It is found in simple_test.php rather than unit_tester.php. See later.html if you want to incorporate other unit tester's test cases in your test suites.
You won't often run single test cases except when bashing away at a module that is having difficulty, and you don't want to upset the main test suite. With autorun no particular scaffolding is needed, just launch your particular test file and you're ready to go.
You can even decide which reporter (for example, TextReporter or HtmlReporter) you prefer for a specific file when launched on its own...
![]() |
![]() |
![]() |
Simple Test PHP Unit Test Framework | Simple Test PHP Unit Test Framework | Group tests |
Documentation generated on Sun, 04 May 2008 09:21:07 -0500 by phpDocumentor 1.3.0