PHPUnit3 テスト実行(CUI)
テストの実行はphpunitコマンド。
公式マニュアル
xdebugがあれば(XAMPPは標準搭載)、カバレッジレポートも出力できる。
PHPUnit3 スケルトンの作成
PHPUnit3。既存のクラスをもとに、スケルトンクラスを作成する。
クイックスタート
既存クラス
MyDate.php
<?php
class MyDate{
function getYear(){
return 2010;
}function getMonth(){
return 7;
}function getDay(){
return 16;
}function getYMDString(){
return “20100716″;
}
}
?>
スケルトンクラスを作成する
phpunitコマンドのskeleton-testオプションを利用
>phpunit –skeleton-test MyDate
PHPUnit 3.4.15 by Sebastian Bergmann.Wrote skeleton for “MyDateTest” to “C:\xampp\htdocs\sample\MyDateTest.
php”.
作成されたMyDateTestクラス
MyDateTest.php
>?php
require_once ‘PHPUnit/Framework.php’;
require_once ‘C:\xampp\htdocs\sample\MyDate.php’;/**
* Test class for MyDate.
* Generated by PHPUnit on 2010-07-18 at 08:57:32.
*/
class MyDateTest extends PHPUnit_Framework_TestCase
{
/**
* @var MyDate
*/
protected $object;/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new MyDate;
}/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}/**
* @todo Implement testGetYear().
*/
public function testGetYear()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
‘This test has not been implemented yet.’
);
}/**
* @todo Implement testGetMonth().
*/
public function testGetMonth()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
‘This test has not been implemented yet.’
);
}/**
* @todo Implement testGetDay().
*/
public function testGetDay()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
‘This test has not been implemented yet.’
);
}/**
* @todo Implement testGetYMDString().
*/
public function testGetYMDString()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
‘This test has not been implemented yet.’
);
}
}
?>
PHPUnit3概要&インストール
PHPUnit3。よりシンプルで強力なユニットテストフレームワーク。よさそう。
環境
-WindowsXP
-XAMPP
-PHPUnit3.4
インストール
C:\xampp\php>pear channel-discover pear.phpunit.de
Adding Channel “pear.phpunit.de” succeeded
Discovery of channel “pear.phpunit.de” succeededC:\xampp\php>pear install phpunit/PHPUnit
Unknown remote channel: pear.symfony-project.com
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, channel:/
/pear.symfony-project.com/YAML, use –alldeps to download automatically
phpunit/PHPUnit can optionally use package “pear/Image_GraphViz” (version >= 1.2
.1)
phpunit/PHPUnit can optionally use package “pear/Log”
phpunit/PHPUnit can optionally use package “channel://pear.symfony-project.com/Y
AML” (version >= 1.0.2)
phpunit/PHPUnit can optionally use PHP extension “xdebug” (version >= 2.0.5)
downloading PHPUnit-3.4.15.tgz …
Starting to download PHPUnit-3.4.15.tgz (255,036 bytes)
……………………………………………..done: 255,036 bytes
install ok: channel://pear.phpunit.de/PHPUnit-3.4.15C:\xampp\php>
インストール手順より。

