Introduction to SimpleTest

Perrick Penet, No Parking

FOSDEM, Bruxelles, 2005

Technical debt and legacy code

Technical debt is a measure of how untidy or out-of-date the development work area for a product is. It builds up to create legacy code.

Is your code easy to change? Can you get nearly instantaneous feedback when you do change it? If the answer to any of these questions is no, you have legacy code, and it is draining time and money away from your development efforts.

Developer testing

You can invest in tools but these don't add value over time : there's no interest over time.

We value individuals and interactions over processes and tools.

Extract from the Agile Manifesto.

One such area is unit and accpetance tests -- developpers' tests : Kent Beck -- Developer Testing.

Definition of unit test : c2 wiki

Definition of acceptance test (or functionnal test) : c2 wiki

Preventing hair loss...

Here's probably what you want to avoid :

...and fighting spam ;-)

Here's probably what automated developer testing can do to you :

Check out other satisfied people.

Confidence and serenity are the magic words here.

You know it works, and you know when it works. So developer testing is good for you.

It may also be a nice way to fight spam at the roots.

Once we eradicate spam about hair loss, we can think of a way of making viagra a thing of the past.

Simple unit testing example

The SimpleTest documentation and tutorial are really good (I know, I have translated them ;-). Exemples come from those : my goal today is to try and convince you reading those...

<?php
require_once('simpletest/unit_tester.php');
require_once('simpletest/reporter.php');
require_once('../classes/log.php');

class TestOfLogging extends UnitTestCase {
    
    function testCreatingNewFile() {
        @unlink('/temp/test.log');
        $log = new Log('/temp/test.log');
        $this->assertFalse(file_exists('/temp/test.log'));
    }
}
$test = &new TestOfLogging();
$test->run(new HtmlReporter());
?>

What do we get?

testoflogging

Fail: testcreatingnewfile->True assertion failed.
1/1 test cases complete. 0 passes and 1 fails.

testoflogging

1/1 test cases complete. 1 passes and 0 fails.

Acceptance testing exemple

<?php
require_once('simpletest/web_tester.php');
require_once('simpletest/reporter.php');

class TestOfFosdem extends WebTestCase {
    
    function setUp() {
        $this->get('http://fosdem.org/2005/index/dev_room_phppear/schedule');
    }
    
    function testMyName() {
        $this->assertWantedPattern('/perrick penet/i');
        $this->assertNoUnwantedPattern('/perrick pennet/i');
    }
}
$test = &new TestOfFosdem();
$test->run(new HtmlReporter());
?>

Test driven development (TDD)

Once you've written a few test, it may seem tedious at the beginning. Then one day something incredible happen : you modify some code somewhere, then run your test suite and a red bar appears at the other end of your code. You were on the fringe of introducing a new bug and it was discovered automatically. From then on, your investment starts paying back.

You're about to become "test infected".

Writing a test before any code is now your new motto. It has a special rythm to it :

write a test
write some code
refactor the code

Well actually, it's more something like :

write a test
write some code
write some working code
refactor messy code
refactor not-so-messy-anymore code
start again with another test

Other benefits of developer testing

For another such list : Keith Ray.

SimpleTest under the bonnet

Right now, there are two active PHP unit testing packages : Pear PHPUnit (Sebastian Bergmann) and SimpleTest (Marcus Baker).

In SimpleTest, you'll find :

The upcoming version (1.1) should bring :

SimpleTest from the outside

TDD is still a early-starter phenomenon in the PHP World.Its founders come from the OO World -- Java in particular, Ruby also has a built-in unit tester.

Open Source projets using SimpleTest :

Projects with * are listed in the Zend PHP5 contest (Prado came 1st and LIMB 3rd). I'm pretty sure it means something !

Developers's tools using SimpleTest :

Questions ? Comments ?

SimpleTest

http://www.lastcraft.com/simple_test.php

Perrick Penet

company : No Parking

blog : :: onpk ::

community : AFUP