单元测试是几个现代敏捷开发方法的基础,使得PHPUnit成为许多大型PHP项目的关键工具。这个工具也可以被Xdebug扩展用来生成代码覆盖率报告 ,并且可以与phing集成来自动测试,最后它还可以和Selenium整合来完成大型的自动化集成测试。
<?php require_once'PHPUnit/Framework.php'; classArrayTestextendsPHPUnit_Framework_TestCase{ publicfunctiontestNewArrayIsEmpty(){ /*CreatetheArrayfixture*/ $fixture=array(); /*AssertthatthesizeoftheArray*fixtureis0*/ $this->assertEquals(0,sizeof($fixture)); } publicfunctiontestArrayContainsAnElement(){ /*CreatetheArrayfixture*/ $fixture=array(); /*AddanelementtotheArray*fixture*/ $fixture[]='Element'; /*Assertthatthesizeofthe*Arrayfixtureis1*/ $this->assertEquals(1,sizeof($fixture)); } } ?>
<?php set_include_path(get_include_path().PATH_SEPARATOR.'./PEAR/'); require_once'Testing/Selenium.php'; require_once'PHPUnit/Framework/TestCase.php'; classGoogleTestextendsPHPUnit_Framework_TestCase{ private$selenium; publicfunctionsetUp(){ $this->selenium=newTesting_Selenium("*firefox", $this->selenium->start(); } publicfunctiontearDown(){ $this->selenium->stop(); } publicfunctiontestGoogle(){ $this->selenium->open("/"); $this->selenium->type("q","helloworld"); $this->selenium->click("btnG"); $this->selenium->waitForPageToLoad(10000); $this->assertRegExp("/GoogleSearch/",$this->selenium->getTitle()); } } ?>