Contenu connexe Similaire à PHPUnit + Xdebug 单元测试技术 (20) PHPUnit + Xdebug 单元测试技术5. 一个例子: Stack.php <?php classStack { private $store; publicfunction__construct() { $this->store = array(); } publicfunctionpush($o) { array_push($this->store, $o); } publicfunctionpop() { returnarray_pop($this->store); } publicfunctioncount() { returncount($this->store); } } 6. 如何保证正确性? 手眼测试… 在程序底部添加一些测试语句 $stack = new Stack; if ($stack) { echo $stack->count(); $stack->push(1111); echo $stack->count(); echo $stack->pop(); echo $stack->count(); } 10. 好主意!! 把测试独立成一个文件, 每次执行一下即可 无需再做 剔除/注释 – 加上/反注释 工作了 StackTest.php ------------------------------------------------------------------------------------------- <?php require_once ‘./Stack.php’; $stack = new Stack; if ($stack) { echo $stack->count(); $stack->push(1111); echo $stack->count(); echo $stack->pop(); echo $stack->count(); } 12. PHPUnit来帮忙 <?php require_once'./Stack.php'; class StackTestextends PHPUnit_Framework_TestCase { public function testCanPushAndPop() { $stack = new Stack(); $this->assertEquals(0, $stack->count()); $stack->push('foo'); $this->assertEquals(1, $stack->count()); $this->assertEquals('foo', $stack->pop()); $this->assertEquals(0, $stack->count()); } } 17. Stack 增加新功能时… <?php classStack { private $store; publicfunction__construct() { $this->store = array(); } publicfunctionpush($o) { array_push($this->store, $o); } publicfunctionpop() { returnarray_pop($this->store); } publicfunctioncount() { returncount($this->store); } public function clean() { $this->store = array(); } } 18. <?php require_once'./Stack.php'; class StackTestextends PHPUnit_Framework_TestCase { public function testCanPushAndPop() { $stack = new Stack(); $this->assertEquals(0, $stack->count()); $stack->push('foo'); $this->assertEquals(1, $stack->count()); $this->assertEquals('foo', $stack->pop()); $this->assertEquals(0, $stack->count()); } public function testCanClean() { $stack = new Stack(); $stack->push('foo'); $this->assertEquals(1, $stack->count()); $stack->clean(); $this->assertEquals(0, $stack->count()); } } 也要增加新测试… 20. xUnit方法的优势 减少验证 ‘代码正确性’ 的工作量 自动化回归测试 避免系统退步 写测试用例可以促使程序员发现边界条件 测试用例本身就是很好的示范代码和文档 写测试用例可以促使程序员真正了解需求 完备的测试可以给程序员以信心 More… 22. 鸭子 “When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” James Whitcomb Riley (1849 – 1916) (Duck typing) 27. Zend Framework 示例 <?php classIndexControllerTestextendsZend_Test_PHPUnit_ControllerTestCase { … publicfunctiontestIndexAction() { $params = array('action' => 'index', 'controller' => 'Index', 'module' => 'default'); $urlParams = $this->urlizeOptions($params); $url = $this->url($urlParams); $this->dispatch($url); // assertions $this->assertModule($urlParams['module']); $this->assertController($urlParams['controller']); $this->assertAction($urlParams['action']); $this->assertQueryContentContains("div#welcome h3", "This is your project's main page"); } } 42. 如何调试一个测试? $ export XDEBUG_CONFIG="remote_host=192.168.8.91idekey=netbeans-xdebug" ## plain old php $ phpunitStackTest ## zend framework $ phpunit application/controllers/FooControllerTest ## symfony 1 $ symfonytest:functionalfoo