The Tricky.net

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:

Unit test

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

Unit fail


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

Comments
Search RSS
question
hello (193.253.245.xxx) 05-03-2012 14:15:01

SimpleTest is great , but i need to know if it is able to be used with a
framwrok such a zend framwork for example ??
gerry (193.63.235.xxx) 03-05-2012 13:46:06

yes you can use simpletest with frameworks
I have used it with zend Boilerplate
with no problems at all.
Anonymous (202.88.253.xxx) 12-02-2013 11:26:45

Anonymous (121.96.171.xxx) 23-08-2011 04:29:20

Only registered users can write comments!

3.23 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."