ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

An introduction to Test Driven Development with PHP

(Page 1 out of 2)

Abstract

This article will introduce you to the concept of Test Driven Development, and demonstrate it with a simple example project.

Introduction

There are several different ways of developing something. Most of us tend to simply start with a script, and gradually move along. Perhaps we've even laid out our script before hand, but we tend to stay at the developing phase, and don't really start testing when it's necessary. Basically, we develop first, and test later.

But this might not be the best way, and might lead to problems later. That's why some developers advocate a different way of developing called Test Driven Development (TDD), where by you test first, and develop later.

You're probably wondering how this works, and that's exactly what we'll discover in this article. I'm going to take you through a really simple example project to demonstrate how TDD works. This article and the example project are based on a forum thread by Noel Darlow ("McGruff") where he shows another forum member how TDD works.

Our example project will be a Biter class that can "bite" sections out of strings, using a regular expression, like this:


$biter->bite ('/pattern/');
?>

Our biter will also alter the original string and remove the part that it found (hence the name "biter").

Let's get started, by setting up our testing framework.

Setting up the testing framework

Since we are starting with testing we'll need to have some sort of testing framework. I'll be using the SimpleTest framework because that's what I'm most familiar with.

Download a copy of SimpleTest, and install it on your localhost or server. Then, create a new file called 'test_biter.php', and put the following code in it:


require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';

class BiterTestCase extends UnitTestCase {
        function testSetup () {
                $this->assertTrue(false);       
        }
}

$test = new BiterTestCase('TDD Biter Test');

$test->run(new HtmlReporter());
?>

Let's go through this example. First, we include a few files of the SimpleTest framework (make sure the path is correct). Then we create a new class called BiterTestCase which will test our Biter class. As you can see, the BiterTestClase class extends the UnitTestCase class, which means the BiterTestClass is now our first real test case.

The BiterTestClass has only one method called 'testSetup'. Any method that starts with 'test' will automatically be run by the SimpleTest framework, and should be a test for some part of your project. In the above example we make sure the framework is setup correctly by calling the assertTrue() method.

The last two lines of the example actually create an instance of our test case, and run all the tests. If everything has been setup correctly you will get the following output:

Now that we've got our testing framework up and running, let's start with our Biter class.

The first test

Before we test anything in our Biter class, we have to make sure that the class exists. We do that with a test of course:


require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';

class BiterTestCase extends UnitTestCase {
        function testClassExists() {
                $this->assertTrue(class_exists('Biter'), 'Biter class exists')
        }
}

$test = new BiterTestCase('TDD Biter Test');
$test->run(new HtmlReporter());

?>

Now run the above test. Don't create the class or include anything else; first run the test. It should fail, since the 'Biter' class doesn't exist, and you should get the following:

As you can see, our tests indicate that the 'Biter' class doesn't exist. In TDD you should always start with a failing test.

Now we have to make sure the test passes, which can be done by creating the Biter class. Create a new file called 'biter.php', and put the following code in it:

Class Biter {

}

?>

Then include the Biter class in our test file, with the following line:

require_once 'biter.php';

Now run the tests again. This time, they should pass, giving the following output:

We've made the first step in creating our Biter class, and we did it in a test driven way. First we made the tests, and then we did the actual development.

But at this point our Biter class doesn't do anything yet, so let's get started with the first part of the Biter: the actual biting.

Next: Applying TDD to create our Biter class »



Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Setting up the test framework & First Test
  2. Applying TDD to create our Biter class
Bookmark Article
Download Article
PDF
Download this article as a PDF file