|
PHP unit testing in eclipse
As testing is really important for the development of any application, I started looking at the testing possibilites in PHP. Today, I'll have a look at the SimpleTest plugin for eclipse.
Installation
You should have eclipse pdt setup already. You can have a look at this article to help you with this.
First install the simpleTest environment.
Install the plugin in eclipse:
- go under help->software updates -> find and install
- create a new remote site with name: simpleTest and url: http://simpletest.org/eclipse/
- click next for installation
- restart eclipse
Now the plugin should be installed. We still have to do some configuration in order for it to work.
- Go under window->preferences->SimpleTest
- Enter the location of your php.exe file (for my wamp installation this was: c:\wamp\bin\php\php5\php.exe)
- Enter the location of your php.ini file (should be in the same folder)
- Enter the location of the folder where you extracted your simpletest files
- Enter .php as a test file suffix
Alternatively you can install phpUnit2 instead of simpletest. But I will stick with simpletest for now.
Creating your first test
Create a file test.php and put the following code int it:
<?php
class test extends UnitTestCase {
function test_pass(){
$boolean = false;
$this->assertFalse($boolean);
}
}
?>
To execute the test, right click -> run as -> SimpleTest
The output should look like this:

If I now put a test that fails, the result will look like this:

I hope you get the idea. Here follow some of the assert functions that simpleTest supports:
| assertTrue($x) |
asserts that $x is true |
| assertFalse($x) |
asserts that $x is false |
| assertEqual($x, $y) |
asserts that $x is equivalent to $y |
| asserNotEqual($x, $y) |
asserts that $x is not equal to $y |
| assertNull($x) |
asserts that $x is null |
| assertNotNull($x) |
asserts that $x is not null |
| assertIsA($x, $t) |
asserts that $x is of type $t |
| assertNotA($x, $t) |
asserts that $x is not of type $t |
| assertWithinMargin($x, $y, $m) |
asserts that |$x-$y| < $m is true |
| assertOutsideMargin($x, $y, $m) |
asserts that |$x-$y| < $m is false |
assertIdentical($x, $y)
|
asserts that $x == $y and that $x and $y are of the same type
|
asserNotIdentical($x, $y)
|
asserts that either $x != $y and/or $x and $y are of a different type
|
assertClone($x, $y)
|
asserts that $x and $y are identical copies
|
assertReference($x, $y)
|
asserts that $x and $y are the same variable
|
assertPattern($p, $x)
|
asserts that the regular expression $p matches $x
|
assertNoPattern($p, $y)
|
asserts that the regular expression $p doesn't match $x
|
expectError($x)
|
swallows any upcoming matching error
|
assert($e)
|
asserts that we get error error $e
|
This should get you starting with unit tests in eclipse and with php ! Of course, you can have tests that are much complexer. You can find very good documentation on that here
|