编程惯用法
PHP测试框架——SimpleTest 。这个测试框架可以在http://simpletest.org获取到。
<?php
// PHP4
// the subject code
define('TAX_RATE', 0.07);
function calculate_sales_tax($amount) {
return round($amount * TAX_RATE, 2);
}
// include test library
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';
// the test
class TestingTestCase extends UnitTestCase {
function TestingTestCase($name = '') {
//$this->TestSalesTax();
echo $name;
}
function TestSalesTax() {
$this->assertEqual(7, calculate_sales_tax(100));
$this->assertEqual(3.5, calculate_sales_tax(50));
}
// function TestSomeMoreSalesTax() {
// $this->assertEqual(3.5, calculate_sales_tax(50));
// }
function TestRandomValuesSalesTax() {
$amount = rand(500, 1000);
$this->assertTrue(defined('TAX_RATE'));
$tax = round($amount * TAX_RATE * 100) / 100;
$this->assertEqual($tax, calculate_sales_tax($amount));
}
}
// run the test
$test = new TestingTestCase('Testing Unit Test');
$test->run(new HtmlReporter());