« Shibuya.pm #12 行ってきました | メイン | かに鍋たべたー »

2010/10/02

Kohanaでのunittest

3系のドキュメントにunittestがないので記録

PHPUnitベースのUnitTest環境構築用モジュールが提供されている。
http://github.com/kohana/unittest
こちらのinstallation参照。
拾ってきたファイルを
modules/unittestとして解凍するだけ
もし入れてなければPHPUnitをPearで入れる。
http://www.phpunit.de/manual/current/en/installation.html
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear install phpunit/PHPUnit 
GithubのほうのKohana::unittestのinstallationに従い、 bootstrapのソースを改変すると http://[yourdomain]/unittest/でTOP画面が見える mod_rewriteによるルーティングをカスタマイズしている場合は、 RewriteCond %{REQUEST_URI} ^/unittest RewriteRule ^(.*) index.php/$1 [PT] と、.htaccessにてunittestだけ標準ルーティングに変更 
実際のテストクラスは application/tests/hoge.php というように、testsディレクトリ下にファイルを置いていく TESTサンプルは以下
<?php
/**
 * @group somegroup.morespecific.annoyingstuff
 * @author shimazu
 *
 */
Class ExampleTest extends Kohana_Unittest_TestCase
{
	
	function setUp(){
		parent::setUp();
	}
	
	function tearDown(){
		parent::tearDown();
	}
	
	function providerStrLen()
	{
		return array(
			array('One set of testcase data', 24),
			array('This is a different one', 23),
		);
	}

	/**
	 * @dataProvider providerStrLen
	 */
	function testStrLen($string, $length)
	{
		$this->assertSame(
			$length,
			strlen($string)
		);
	}
	
	/**
	 * @dataProvider providerStrLen
	 */
	function testError($string, $length){
		$this->assertSame(
			$length,
			strlen($string) + 1
		);
		
	}
}

 javadoc形式でのアノテーションにてグループ指定などが可能。
データプロバイダなんておしゃれなものも同様に指定可能
実行結果は以下

 

コメント

フィード You can follow this conversation by subscribing to the comment feed for this post.

この記事へのコメントは終了しました。