.
Developer Spot - Web Development Tutorials
arrowDeverloper Spot  Tutorials  PHP  Test Driven Development 
 
Development Tutorials
ASP
CGI & Perl
CSS
HTML
Java
JavaScript
Linux
PHP
XML




More Resources
Web Hosting Articles
Web Development News
PHP Manual
Create a Survey
Web Design Tutorials
Flash Tutorials
Rank Detective
Web Hosting Directory
Budget Web Hosting Linux Web Hosting Small Business Hosting
Windows Web Hosting Reseller Web Hosting Web Hosting Articles

Test Driven Development

By Marcus Baker
2004-01-25
Reader Rating: 5 out of 5
Bookmark Print Version
Minimal Code

The catch is that we are not going to design the code in any way at all. We are going to write only enough to pass the test. Here is the code...

class ConfigurationParser {
    function parse() {
        return array();
    }
}

This is after all the bare minimum to get to green. If you were tempted to plan ahead as to how implement a parser, then you might want to keep your wrists out of site after all. Our test has no trouble passing...


configurationtest


1/1 test cases complete:1 passes, 0 fails and 0 exceptions.

If you are losing patience right now, don't worry. The pace will now pick up.

The first test was just to get the structure up. We'll now genuinely constrain the solution with a one line configuration...

class ConfigurationTest extends UnitTestCase {
    function ConfigurationTest() {
        $this->UnitTestCase();
    }
    function testNoLinesGivesEmptyHash() {
        $parser = &new ConfigurationParser();
        $this->assertIdentical($parser->parse(array()), array());
    }
    function testKeyValuePair() {
        $parser = &new ConfigurationParser();
        $this->assertEqual(
            $parser->parse(array("a A\n")),
            array('a' => 'A'));
    }
}

First we'll do whatever we can to get to green...

class ConfigurationParser {
    function parse($lines) {
        if ($lines == array("a A\n")) {
            return array('a' => 'A');
        }
        return array();
    }
}

This works, but the design sucks. Adding more if statements is hardly the solution for each test. It will only work for these tests, be repetitive and the code doesn't really explain what we are trying to do. Let's fix it next.


Article Pages:
Test Driven Development
Writing a Test
A Simple Test Case
Our first test (at last)
Minimal Code
Design with working tests
Tests as documentation
Test then Code then Design

 Rate this article:   Poor          Excellent 


If you found this article interesting, you may want to read these as well:

» Protecting your PHP and HTML Source Code

» Publishing Newsletters Using PHP & MySQL - 4

» Publishing Newsletters Using PHP & MySQL - 3

» Publishing Newsletter Using PHP & MySQL - 2

» Publishing Newsletters Using PHP & MySQL

» Unix Webserver Crontab Basics



 
Development Tutorials: CGI & Perl - CSS - HTML - Java - JavaScript - Linux - PHP - XML
More Resources: Web Hosting Articles - Web Development News - PHP Manual