23
Mar

Unittests with PHPUnit

categories Computer, PHP, Tutorials    

A little private encounter with the community of an open source project opened my eyes, how good and easy life as a coder is if you’re working with unit tests. Not everybody uses them, but everybody should. For php programming the ideal software for this is PHPUnit.

The software can be obtained from GitHub verteilt, is well documented, widely used and very popular. It is also in the PEAR project and can be installed using the PEAR installer (it has some dependencies for additional features).

To demonstrate the basic procedural manner I’ll use a simple example. Imagine… somewhere within a php project there is a function, which returns easter sunday given any year. To make things easy, we’ll name it easter($year). Such a function could be used for determining holidays and therefore could have a certain criticality. How can this be tested with PHPUnit?

Let’s delve into code:

require_once 'includes/functions.php';
 
class EasterTest extends PHPUnit_Framework_TestCase {
  public function testEasterOk() {
    $easter2011 = easter(2011);
    $this->assertEquals($easter2011, gmmktime(0,0,0,4,24,2011));
  }
 
  public function testEasterFalse() {
    $easter2011 = easter(2011);
    $this->assertNotEquals($easter2011, gmmktime(0,0,0,12,24,2011));
  }
} 

First there is a link to the script containing the easter-function (here: functions.php). Then I generate a new class (“EasterTest”), which extends PHPUnit. In this class there are two basic tests:

Now to the boring theoretical part…
The solution isn’t the aim here – one glance at a calender should help you way better. The goal is, to compare the functions result with a defined result. I know for sure, that this years easter sunday is on 24.04. (as long as my calendar isn’t lying!) … und I know for sure, 24.12. ain’t somewhere near easter.

PHPUnit – like many other software products around unit testing – works with “assertions”. There is a library of simple functions for assertions in different constructs – for comparisms (like the here used assertEquals and assertNotEquals), arrays, bools and so on.

How much work is it?
Please decide for yourself. An easy test should be coded within less than five minutes. Not too difficult, ain’t it?

Back to work.

How can you now run those tests? Easiest way: use your shell (whatever, either Dos, Bash or anything else php runs on).

D:\htdocs\>phpunit EasterTest.php
PHPUnit 3.5.11 by Sebastian Bergmann.

..
 
Time: 0 seconds, Memory: 3.75Mb
 
OK (2 tests, 2 assertions)

PHPUnit can express itself using different output formats, for example TAP (“Test Anything Protocol”), JSON, XML and some more.

D:\htdocs\>phpunit --tap EasterTest.php
TAP version 13
ok 1 - EasterTest::testEasterOk
ok 2 - EasterTest::testEasterFalse
1..2

Now… how does it look like if there is an error? To test this I slighly changed my first test and assumed, 23.04.2011 is easter sunday – in reality it is the saturday.

D:\htdocs\>phpunit --tap EasterTest.php
TAP version 13
not ok 1 - Failure: EasterTest::testEasterOk
  ---
  message: 'Failed asserting that <integer:1303516800> matches expected <integer:1303603200>.'
  severity: fail
  data:
    got: 1303516800
    expected: 1303603200
  ...
ok 2 - EasterTest::testEasterFalse
1..2

If I return coding on the easter-function in the far future, then I still have my unit tests and can run them whenever I like (or whenever I need!). If the tests then return errors, either my tests are wrong or my function is (guess what is more likely). And this is what this is all about. Unit testing makes your code stronger and additions much easier.

And…if you encounter bugs… just write a test first and then fix it!

Kommentare

Leave a comment!