PHP Development: A Test-Driven

All Code is Guilty Until Proven Innocent – http://net.tutsplus.com/

Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that the good programmer uses tests to detect his mistakes as soon as possible. The sooner you test for a mistake the greater your chance of finding it and the less it will cost to find and fix. This explains why leaving testing until just before releasing software is so problematic. Most errors do not get caught at all, and the cost of fixing the ones you do catch is so high that you have to perform triage with the errors because you just cannot afford to fix them all. That is why software test is very important in Software Engineering, including web.

Test-driven development is a programming technique that requires you to write actual code and automated test code simultaneously. This ensures that you test your code—and enables you to retest your code quickly and easily, since it’s automated.

Test-driven development (TDD) revolves around a short iterative development cycle that goes something like this:

  1. Before writing any code, you must first write an automated test for your code. While writing the automated tests, you must take into account all possible inputs, errors, and outputs. This way, your mind is not clouded by any code that’s already been written.
  2. The first time you run your automated test, the test should fail—indicating that the code is not yet ready. If the first time you run your automated test success, it means that your test code is wrong.
  3. Afterward, you can begin programming. Since there’s already an automated test, as long as the code fails it, it means that it’s still not ready. The code can be fixed until it passes all assertions.
  4. Once the code passes the test, you can then begin cleaning it up, via refactoring. As long as the code still passes the test, it means that it still works. You no longer have to worry about changes that introduce new bugs. You still can re-run your automated tests anytime.
  5. Start the whole thing over again with some other method or program.

PHP Code Testing Example

<?php 

$data = array(); 
assertTrue(count($data) == 0); 
$data[] = 'element'; 
assertTrue(count($data) == 1);

function assertTrue($condition) { 
  if (!$condition) { 
    throw new Exception('Assertion failed.'); 
  } 
} 

?>

The test is now completely automated. so we have an automated test. The goal of using automated tests is to make fewer mistakes. While your code will still not be perfect, even with excellent tests, you will likely see a dramatic reduction in defects once you start automating tests. Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in design (Refactoring), get along with your teammates better (Cross-Team Tests), improve relations with your customers, and go home every night with proof that the system is better now than it was this morning because of your efforts.

Adapted from:
http://net.tutsplus.com/tutorials/php/the-newbies-guide-to-test-driven-development/ 
http://www.phpunit.de/manual/current/en/index.html

3 Replies to “PHP Development: A Test-Driven”

  1. TDD make your life easier, you don’t need to test it manually or even opening your browser to make sure your code is right. Of course you need to write your code in OOP. In PHP you can use PHPUnit to test your code, by combining PHPUnit with xdebug you can see your code coverage to see which part of your code has been tested or not.
    By the way thanks for the nice info, may be you can include PHPUnit example.

      • PHP can be helpful in automated testing. But it depends on what to test. Basically automated testing is doing several automated function calls with various kind of arguments and check whether the resulting value of called function is correct or not.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

*