class Account{ var $balance; function Account($initialBalance=0){ $this->balance = $initialBalance; } function withdraw($amount){ $this->balance -= $amount; } function deposit($amount){ $this->balance += $amount; } function getBalance(){ return $this->balance; } function transferFrom(&$sourceAccount,$amount){ $sourceAccount->withdraw($amount); $this->deposit($amount); } ?>
class AccountTester extends TestCase{ var $_ac1; var $_ac2; var $_ac3; var $_ac4;
function AccountTester($name){ $this->TestCase($name); // call parent constructor } function setUp(){ $this->_ac1 = new Account(100); // data for testWithdraw $this->_ac2 = new Account(20); // data for testDeposit $this->_ac3 = new Account(30); // data for testTransferFrom $this->_ac4 = new Account(50); } } ?>
加入专门的测试代码 现在,我们可以往向AccountTester类加入测试代码了。
<?php
// Make a withdrawal of 25 units from _ac1. // _ac1's initial balance is 100
<?php $tSuite = new TestSuite(); //creation of the test suite object 创建测试套件对象 $tSuite->addtest(new AccountTester("testWithdraw")); //Add inidividual tests $tSuite->addtest(new AccountTester("testDeposit")); //加入专门测试方法。 $tSuite->addtest(new AccountTester("testTransferFrom")); $res = new TextTestResult(); //Creation of the Result 建立一个测试结果类 $tSuite->run(&$res); //Run of the test 运行测试 $res->report(); //Print results 输出测试结果。 ?>