phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Simple Test: Testing Your Code (Part 1)

Tag: simple test, test unit   Category:
post: 11 Nov 2007 read: 807


It is important to test your code. You will release stress by doing good testing your code. Imagine, you almost done your complicated application. But, oh no.. Your boss find a bug. You must trace all of your code to find this bug. It will better you test every part of your application.

Amongst software testing tools, a unit tester is the one closest to the developer. In the context of agile development the test code sits right next to the source code as both are written simultaneously. In this context SimpleTest aims to be a complete PHP developer test solution and is called "Simple" because it should be easy to use and extend.

You can download simpletest at www.simpletest.org. After download, you can follow this tutorial:

  1. Extract you compressed simple test file to a folder name "simpletest" within "www/test/". In this tutorial, I use simpletest_1.0.1beta2.tar.gz.
  2. Assuming, you are developing a credit application. You write calculation interest per year, like this:
    
    <?php
    define('RATE', 0.05);
    
    function interest_per_year($amount)
    {
    	round($amount * RATE, 2);
    }
    ?>
    

    create a file name "test.php" for example within "www/test/". Enter following code:

    
    <?php
    
    define('RATE', 0.05);
    
    function interest_per_year($amount)
    {
      round($amount * RATE, 2);
    }
    
    // Include test library
    require_once 'simpletest/unit_tester.php' ;
    require_once 'simpletest/reporter.php' ;
    
    class TestingTestCase extends UnitTestCase{
    
      function TestingTestCase($name = '')
      {
        $this->UnitTestCase( $name );
      }
    	
      function TestInterest(){
        $this->assertEqual(5, interest_per_year(100));
      }
    	
    }
    
    // run test
    $test = new TestingTestCase( 'Testing Unit Test' );
    $test->run(new HTMLReporter());
    
    ?>
    

Point your browser to http://localhost/test/test.php, you will get:

Why you still get error? Your function do not return any value:


<?php
define('RATE', 0.05);

function interest_per_year($amount)
{
  round($amount * RATE, 2);
}
?>

So, your following this will get false:


  function TestInterest(){
    $this->assertEqual(5, interest_per_year(100));
  }	

Now, change and add return at your interest_per_yer():


<?php
define('RATE', 0.05);

function interest_per_year($amount)
{
  return round($amount * RATE, 2);
}
?>

Reload page again:



Series this article:
Simple Test: Testing Your Code (Part 1)
Simple Test: Testing with Several Values (Part 2)

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
CAPTCHA - part 3 : "Are you human or ....?" (Build Your CAPTCHA)

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting