| « Chart: How to Build Cool Animation Real Time Chart | Simple Test: Testing with Several Values (Part 2) » |
Simple Test: Testing Your Code (Part 1)
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:
- 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.
- 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) blog comments powered by Disqus |

