About the Author

Chris Shiflett

Hi, I'm Chris, a web developer and a founding member of Analog. I live and work in Brooklyn, NY.


Who Practices Test-Driven Development (TDD)?

Harry Fuecks maintains a good blog over at Sitepoint and recently wrote a piece on Evaluating PHP Applications.

Noel Darlow, a regular contributor to the Sitepoint forums (and someone whose opinion I respect), comments:

I think testing is a good indicator of the developer's ability. I'll be looking for tests being used to drive the design and not just the odd unit test stuck on after the fact.

I'm a big advocate of testing, and although I won't claim to be an expert on the topic, I have to question whether it's necessary that tests drive the design. It seems plausible that someone could choose to write tests after the implementation, and I don't think ignorance is necessarily the reason.

On the other hand, I find myself taking this approach more and more. For example, when I'm designing a function or class, I start with an example that describes how I want to use it:

<?php 

$auth 
= new myAuth;

$auth->username 'chris';
$auth->password 'mypass';

if (
$auth->checkLogin()) {
    
/* SUCCESS */
} else {
    
/* FAILURE */
}

?>

(Sorry if my ad hoc example doesn't live up to your standards.)

I'll usually type this out at least once, so it's not just imagined. In order to implement myAuth, I can choose to keep this example in mind as I write the code, or I can take this simple example and turn it into a real test or two:

<?php 

include 'test-more.php';

plan(2);

$auth = new myAuth;

{
    
/* Test Valid Credentials */
    
$auth->username 'chris';
    
$auth->password 'mypass';

    
ok($auth->checkLogin(), 'test valid credentials');
}

{
    
/* Test Invalid Credentials */
    
$auth->username 'chris';
    
$auth->password 'notmypass';

    
ok(!$auth->checkLogin(), 'test invalid credentials');
}

?>

It's so easy to write tests, I might even write some for a blank username, blank password, etc. I can do all this without writing a single line of code, and when I begin writing the code, I already have a simple test suite to run - I don't have to write some quick ad hoc tests just to see whether things are working as planned. When all the tests pass, I know I've accomplished my initial design goals.

How many of you test? How many of you write your tests first?

Note: If you happen to be attending PHP Quebec later this week, I'm giving a talk that will discuss some simple approaches to testing.

About This Post

Who Practices Test-Driven Development (TDD)? was posted on Tue, 28 Mar 2006 at 03:18:40 GMT.

0 Comments

Post A Comment

Personal Details and Comment

Style Guide

Line breaks are converted to paragraphs. Also use:

  • <a href="" title="">text</a>1
  • <em>text</em>
  • <blockquote><p>text</p></blockquote>
  • <code>2  <?php  if ($foo) {      $foo = TRUE;  }  ?></code>
  1. Note: <code> can be used inline (e.g. in paragraphs) or in a block as shown. Include whitespace and newlines in blocks.

Please enter Chris (my first name) below. This is a primitive spam prevention technique, and I apologize for the inconvenience.

Preview and Submit

Upcoming Talks

ConFoo

10 - 12 Mar 2010

At Hilton Montréal Bonaventure, Montréal, Canada.

South by Southwest

12 - 16 Mar 2010

At Austin Convention Center, Austin, Texas.

Dutch PHP Conference

10 - 12 Jun 2010

At TBD, Amsterdam, Netherlands.

O'Reilly Open Source Convention

19 - 23 Jul 2010

At Oregon Convention Center, Portland, Oregon.

New Comments

liukang wrote:

I have problem with this example. In my php.ini magic_quotes_gpc is off so i'm using only addsla...

Posted in addslashes() Versus mysql_real_escape_string()
RyanTheGreat wrote:

Well, I'm not Chris, but I will do my best to address the questions raised in the comments by Ian...

Posted in Security Corner: Cross-Site Request Forgeries
Chris Shiflett wrote:

Thanks for the kind words, Simon. I'm glad you liked the tutorial. In case it's helpful, here'...

Posted in Webstock
Chris Shiflett wrote:

Hi Robin, I plan to post something about it, but it's going to be hard to express everything i...

Posted in Webstock
Simon Mahony wrote:

Hi Chris, I really enjoyed your workshop on the Evolution of Security at Webstock. I think I g...

Posted in Webstock

Browse Comments


Work and Books

Analog Essential PHP Security HTTP Developer's Handbook