PHPUnitの簡単なテストコード
以下サンプルコード
実装する予定のクラスは以下のようなことができる
% vi test/lib/MyApp.php
<?php require_once( dirname(__FILE__) . './../../lib/MyApp.class.php' ); class MyAppTest extends PHPUnit_Framework_TestCase { public function testHello () { $this->assertEquals( MyApp::hello('hoge'), 'hello, hoge'); $this->assertEquals( MyApp::hello('fuga'), 'hello, fuga'); } } ?>
実装したクラスは以下のとおり
vi lib/MyApp.class.php
<?php class MyApp { public function hello($string) { return "hello, $string"; } } ?>
実行結果
:!phpunit test/lib/MyApp.php PHPUnit 3.5.10 by Sebastian Bergmann. . Time: 0 seconds, Memory: 2.50Mb OK (1 test, 2 assertions)
phpunitはもっとたくさんの機能があるようですが、まあassertEqualsだけでも結構保守性高められるんじゃないかと。