最近自分のソースにバグが多いことが嫌になってきたので、テストをきちんと書いてみようかと思ってPHPUnitを導入してみようかと思い、その作業ログを残してみました。
そもそもPHPUnitなんて使ったこともなければ、まともに調べたこともなかった。
PHPUnit自体はPHPUnit、PHPUnit2、PHPUnit3とあり、PHPUnitはPHP4用、PHPUnit2とPHPUnit3はPHP5用として配布されているみたいです。
PHPUnit3はPHPのバージョンやPEARのバージョン等がかなり新しくないと入らないみたいなので、
PHPUnit3を入れられる要件を満たしていない場合は無理に入れる必要はないかもしれません。
今回は調べながらいろいろやってたので、無駄にPHPUnit→PHPUnit2→PHPUnit3とインストールと削除を繰り返してしまいました…。
で、作業ログ。
とりあえず、PHPのバージョンをアップデートして、PHPUnitのインストールを試みる。
まずPHPUnitはソースを落としてきて置いただけ。
PHP4用に書かれてるんで参照渡しのところでWarningが出てた。
これしかないのかと思って調べているとPHPUnit2ってのがあることに気付いた。
さらに、PHPのバージョンを上げればPHPUnit3が使えるみたいなので、
それを使ってみることにした。
# pear channel-discover pear.phpunit.de
Channel "pear.phpunit.de" is already initialized
既に追加済みでした。
で、インストール
# pear install phpunit/PHPUnit
Did not download optional dependencies: ezc/ConsoleTools, use --alldeps to download automatically
phpunit/PHPUnit requires PEAR Installer (version >= 1.9.2), installed version is 1.9.1
phpunit/PHPUnit can optionally use PHP extension "dbus"
phpunit/File_Iterator requires PEAR Installer (version >= 1.9.2), installed version is 1.9.1
phpunit/PHP_CodeCoverage requires package "phpunit/File_Iterator" (version >= 1.2.2)
phpunit/PHPUnit_MockObject can optionally use PHP extension "soap"
downloading DbUnit-1.0.1.tgz ...
Starting to download DbUnit-1.0.1.tgz (39,055 bytes)
......done: 39,055 bytes
downloading Text_Template-1.1.0.tgz ...
Starting to download Text_Template-1.1.0.tgz (2,783 bytes)
...done: 2,783 bytes
downloading PHP_Timer-1.0.0.tgz ...
Starting to download PHP_Timer-1.0.0.tgz (2,536 bytes)
...done: 2,536 bytes
downloading PHPUnit_MockObject-1.0.8.tgz ...
Starting to download PHPUnit_MockObject-1.0.8.tgz (18,199 bytes)
...done: 18,199 bytes
downloading PHPUnit_Selenium-1.0.2.tgz ...
Starting to download PHPUnit_Selenium-1.0.2.tgz (16,412 bytes)
...done: 16,412 bytes
downloading YAML-1.0.5.tgz ...
Starting to download YAML-1.0.5.tgz (9,966 bytes)
...done: 9,966 bytes
downloading XML_RPC2-1.0.8.tgz ...
Starting to download XML_RPC2-1.0.8.tgz (67,063 bytes)
...done: 67,063 bytes
downloading PHP_TokenStream-1.0.1.tgz ...
Starting to download PHP_TokenStream-1.0.1.tgz (7,250 bytes)
...done: 7,250 bytes
downloading Cache_Lite-1.7.10.tgz ...
Starting to download Cache_Lite-1.7.10.tgz (30,348 bytes)
...done: 30,348 bytes
downloading ConsoleTools-1.6.1.tgz ...
Starting to download ConsoleTools-1.6.1.tgz (869,994 bytes)
...done: 869,994 bytes
downloading Base-1.8.tgz ...
Starting to download Base-1.8.tgz (236,357 bytes)
...done: 236,357 bytes
install ok: channel://pear.phpunit.de/Text_Template-1.1.0
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.0
install ok: channel://pear.phpunit.de/PHPUnit_Selenium-1.0.2
install ok: channel://pear.symfony-project.com/YAML-1.0.5
install ok: channel://pear.php.net/Cache_Lite-1.7.10
install ok: channel://components.ez.no/Base-1.8
install ok: channel://pear.phpunit.de/DbUnit-1.0.1
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.0.8
ERROR: unable to unpack /tmp/pear/download/XML_RPC2-1.0.8.tgz
install ok: channel://components.ez.no/ConsoleTools-1.6.1
install ok: channel://pear.phpunit.de/PHP_TokenStream-1.0.1
失敗…。
pear listでPEARのバージョンを確認してみると
# pear list
Installed packages, channel pear.php.net:
=========================================
Package Version State
PEAR 1.9.1 stable
1.9.1か…。
しかたないのでアップデート
# pear upgrade PEAR
downloading PEAR-1.9.2.tgz ...
Starting to download PEAR-1.9.2.tgz (295,120 bytes)
............................................................done: 295,120 bytes
downloading Archive_Tar-1.3.7.tgz ...
Starting to download Archive_Tar-1.3.7.tgz (17,610 bytes)
...done: 17,610 bytes
downloading Structures_Graph-1.0.4.tgz ...
Starting to download Structures_Graph-1.0.4.tgz (30,318 bytes)
...done: 30,318 bytes
downloading Console_Getopt-1.3.1.tgz ...
Starting to download Console_Getopt-1.3.1.tgz (4,471 bytes)
...done: 4,471 bytes
downloading XML_Util-1.2.1.tgz ...
Starting to download XML_Util-1.2.1.tgz (17,729 bytes)
...done: 17,729 bytes
upgrade ok: channel://pear.php.net/Archive_Tar-1.3.7
ERROR: unable to unpack /tmp/pear/download/Structures_Graph-1.0.4.tgz
upgrade ok: channel://pear.php.net/Console_Getopt-1.3.1
upgrade ok: channel://pear.php.net/XML_Util-1.2.1
upgrade ok: channel://pear.php.net/PEAR-1.9.2
PEAR: Optional feature webinstaller available (PEAR's web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR's PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR's PHP-GTK2-based installer)
PEAR: To install optional features use "pear install pear/PEAR#featurename"
もう一回インストール
# pear install phpunit/PHPUnit
phpunit/PHPUnit can optionally use PHP extension "dbus"
downloading PHPUnit-3.5.13.tgz ...
Starting to download PHPUnit-3.5.13.tgz (118,553 bytes)
..........................done: 118,553 bytes
downloading File_Iterator-1.2.6.tgz ...
Starting to download File_Iterator-1.2.6.tgz (3,439 bytes)
...done: 3,439 bytes
downloading PHP_CodeCoverage-1.0.4.tgz ...
Starting to download PHP_CodeCoverage-1.0.4.tgz (115,029 bytes)
...done: 115,029 bytes
downloading XML_RPC2-1.0.8.tgz ...
Starting to download XML_RPC2-1.0.8.tgz (67,063 bytes)
...done: 67,063 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.2.6
install ok: channel://pear.php.net/XML_RPC2-1.0.8
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.0.4
install ok: channel://pear.phpunit.de/PHPUnit-3.5.13
完了。
page_adsence
2011年6月27日月曜日
2011年5月30日月曜日
PHPUnit3の使い方
前回に引き続きPHPUnit3に関してのメモ。
早速PHPUnit3を使ってみた。
んで、保存してテストしてみる。
テスト自体に問題はなさそうだけど、PHP Noticeが出てる…。
なんでだろうと思って調べてみたら、Framework.phpからAutoload.phpに変ったらしい。
「Framework.php」をrequireしても動くが、上記のようにNoticeが出る。
Noticeが出てるのもいやなので、「require_once 'PHPUnit/Framework.php';」→「require_once 'PHPUnit/Autoload.php';」に書き換えてやる。
するとNoticeが出なくなる。
これで気持ちよく使えそうだ。
早速PHPUnit3を使ってみた。
$ vi ArrayTest.php
<?php
require_once 'PHPUnit/Framework.php';
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testNewArrayIsEmpty()
{
// 配列を作成します。
$arr= array();
// 配列のサイズは 0 です。
$this->assertEquals(0, count($arr));
}
/**
* メソッド名がtestで始まっていないが,「@test」
* アノテーションを使用していることに注意
*
* @test
*/
public function arrayContainsAnElementTest()
{
// 配列を作成します。
$arr= array();
// 配列にひとつの要素を追加します。
$arr[] = 'hoge';
// 配列のサイズは 1 です。
$this->assertEquals(1, count($arr));
}
}んで、保存してテストしてみる。
$ phpunit ArrayTest
PHP Notice: Please no longer include "PHPUnit/Framework.php". in /usr/share/pear/PHPUnit/Framework.php on line 50
PHPUnit 3.5.13 by Sebastian Bergmann.
..
Time: 1 second, Memory: 3.25Mb
OK (2 tests, 2 assertions)テスト自体に問題はなさそうだけど、PHP Noticeが出てる…。
なんでだろうと思って調べてみたら、Framework.phpからAutoload.phpに変ったらしい。
「Framework.php」をrequireしても動くが、上記のようにNoticeが出る。
Noticeが出てるのもいやなので、「require_once 'PHPUnit/Framework.php';」→「require_once 'PHPUnit/Autoload.php';」に書き換えてやる。
$ vi ArrayTest.php
<?php
require_once 'PHPUnit/Framework.php';
↓
require_once 'PHPUnit/Autoload.php';
class ArrayTest extends PHPUnit_Framework_TestCase
{
public function testNewArrayIsEmpty()
{
// 配列を作成します。
$arr= array();
// 配列のサイズは 0 です。
$this->assertEquals(0, count($arr));
}
/**
* メソッド名がtestで始まっていないが,「@test」
* アノテーションを使用していることに注意
*
* @test
*/
public function arrayContainsAnElementTest()
{
// 配列を作成します。
$arr= array();
// 配列にひとつの要素を追加します。
$arr[] = 'hoge';
// 配列のサイズは 1 です。
$this->assertEquals(1, count($arr));
}
}$ phpunit ArrayTest
PHPUnit 3.5.13 by Sebastian Bergmann.
..
Time: 1 second, Memory: 3.25Mb
OK (2 tests, 2 assertions)するとNoticeが出なくなる。
これで気持ちよく使えそうだ。
登録:
投稿 (Atom)