PHP Development: A Test-Driven

All Code is Guilty Until Proven Innocent –

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


$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: