Testing PHP

I’m organising the PHP Test Fest that is happening in Manchester next weekend, and in preparation I decided it was high time to sit down and figure out what testing PHP is all about. People kept telling me it was easy but I had no clear picture of how all the pieces went together – there are different ways of doing the same thing and although I have been keen to get involved with testing for some time, I haven’t been able to get started until now.

I’m a linux user, so this whole post relates to how this works for me, on my ubuntu system.

First of all I grabbed a copy of PHP from http://snaps.php.net. The TestFest focuses on writing tests for PHP 5.3 so I took the most recent copy of that version.

I unzipped the code, changed into that directory and compiled the code and ran all the tests:

./configure  --enable-gcov
make
make test

At this point I was prompted to send the report of the tests off to the PHP QA team – I always make sure to do this when I compile PHP, it helps them a lot to collect the data and it is no bother for me. I will say that due to the huge increase in the number of tests in PHP 5.3, it takes a bit longer than it used to, but I don’t mind.

One thing which I keep seeing when I see people talk about tests is the lcov test coverage graphs, the ones that look something like this:

“Just pick something to test from there”, people said. I just didn’t understand at all how to pick which bit of PHP I would need to write test code for. It turns out that if you click on an extension, you get a list of files, and these too have coverage numbers.

Then if you click on an individual file, you see the C code of PHP itself there, with highlights to show which lines are executed in tests and which are not. So now you can start to get an idea of what needs testing (although you have to read C to do it which is fine since I’ve studied it, and PHP is pretty similar, but it does get confusing in places).

It is pretty easy to run a subset of tests, despite some quite long-winded documentation on the subject. There is a script inside the PHP directory called run-tests.php which takes an argument of either a single test or a directory of tests you’d like to run. Just call this with CLI PHP and specify which tests you want – I specify the path to the PHP since I want to use PHP 5.3 and not the existing PHP which I already use on my system

export TEST_PHP_EXECUTABLE=/sapi/cli/php
$TEST_PHP_EXECUTABLE /run-tests.php tests/ext/spl/

The “make test” command also takes a TESTS= argument, and this is also an option. Its just a wrapper for run-tests though so either works fine. I built my php inside the directory with the source in it, just for convenience, but you don’t have to – and if you don’t then you need to adjust the paths above accordingly.

Then its time to read the phpt documentation and start writing tests. There are groups all over the world taking part in the testfest, so check the list and find your nearest. For me that’s Manchester, 9th May 2009 – and I’m looking forward to it!

PHPNW Test Fest Event – May 9th 2009

The PHPNW user group are taking part in this year’s PHP Test Fest. Our event will be on 9th May – you can sign up to attend (limited places so do let us know) on the upcoming page for this event: http://upcoming.yahoo.com/event/2299548/

The idea behind Test Fest is twofold: firstly, to give an approachable way for PHP developers to contribute back to the core – we’ll learn how to write tests on the day and can continue to do so after the event of course! Secondly, PHP itself gets better tests, and the language itself gets better. All in all, I can’t see a reason not to do it!

There are events right around the world so if you can’t be in Manchester then take a look around and see if there is something nearer to you that you could be involved in. Northen people – I’ll see you there :)