PHPUnitを使ってユニットテストをする
スポンサーリンク
PHPのテストには、phpunitを使うことでユニットテストを作成し、ユニットテストを行うことができます。
PHPUnit2をインストールする
FreeBSDはportsからPHPUnit2をインストールすることができます。
kstring.php
phpunitが出力したスケルトン kstringTest.phpの内容
とりあえず、スケルトンをそのまま実行してみると以下の通りになる。
実際にテストコードを作る。
1はやることがないので、 throw new PHPUnit2_Framework_IncompleteTestError; を消してしまう。
実行例
例外処理のテストをする方法について例をあげる。
テストするコード kcrypt.php
phpunitコマンドを使って、kcrypt.phpからスケルトンを生成する。
生成されたスケルトン kcryptTest.php
スケルトン kcryptTest.php を書き換えた結果
kcryptTest.phpの実行結果
1つ失敗をわざとさせてみたときの結果。
サンプル
下記ディレクトリに例があります。
PHPUnit2をインストールする
FreeBSDはportsからPHPUnit2をインストールすることができます。
cd /usr/ports/devel/pear-PHPUnit2 sudo make install clean元になるソース
kstring.php
<?php
class kstring
{
public function __construct () {}
public function __destruct () {}
public function cmp ($s1, $s2) {
return ($s1 == $s2);
}
public function cat ($s1, $s2) {
return ($s1 . $s2);
}
}
?>
% phpunit --skeleton kstring PHPUnit 2.3.4 by Sebastian Bergmann. Wrote test class skeleton for kstring to kstringTest.php.
phpunitが出力したスケルトン kstringTest.phpの内容
<?php
// Call kstringTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
define("PHPUnit2_MAIN_METHOD", "kstringTest::main");
}
require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";
// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";
require_once "kstring.php";
/**
* Test class for kstring.
* Generated by PHPUnit2_Util_Skeleton on 2007-08-24 at 00:16:28.
*/
class kstringTest extends PHPUnit2_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once "PHPUnit2/TextUI/TestRunner.php";
$suite = new PHPUnit2_Framework_TestSuite("kstringTest");
$result = PHPUnit2_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement test__destruct().
*/
public function test__destruct() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
/**
* @todo Implement testCmp().
*/
public function testCmp() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
/**
* @todo Implement testCat().
*/
public function testCat() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
}
// Call kstringTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "kstringTest::main") {
kstringTest::main();
}
?>
とりあえず、スケルトンをそのまま実行してみると以下の通りになる。
% phpunit kstringTest.php 0:20[higex ~/htdocs/dev/php/phpunit] php in free(): warning: junk pointer, too high to make sense PHPUnit 2.3.4 by Sebastian Bergmann. III Time: 0.004827 There were 3 incomplete test cases: 1) test__destruct(kstringTest) 2) testCmp(kstringTest) 3) testCat(kstringTest) OK, but incomplete test cases!!! Tests run: 3, Incomplete Tests: 3.
実際にテストコードを作る。
1はやることがないので、 throw new PHPUnit2_Framework_IncompleteTestError; を消してしまう。
1) test__destruct(kstringTest)
下記の部分のテストコードを実装する。
2) testCmp(kstringTest)
3) testCat(kstringTest)
スケルトンを編集した結果
<?php
// Call kstringTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
define("PHPUnit2_MAIN_METHOD", "kstringTest::main");
}
require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";
// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";
require_once "kstring.php";
/**
* Test class for kstring.
* Generated by PHPUnit2_Util_Skeleton on 2007-08-24 at 00:16:28.
*/
class kstringTest extends PHPUnit2_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once "PHPUnit2/TextUI/TestRunner.php";
$suite = new PHPUnit2_Framework_TestSuite("kstringTest");
$result = PHPUnit2_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement test__destruct().
*/
public function test__destruct() {
}
/**
* @todo Implement testCmp().
*/
public function testCmp() {
$o = new kstring ();
$this->assertEquals (true, $o->cmp ('abc', 'abc') );
$this->assertEquals (false, $o->cmp ('abc', 'def') );
}
/**
* @todo Implement testCat().
*/
public function testCat() {
$o = new kstring ();
$this->assertEquals ('abcdef', $o->cat ('abc', 'def') );
}
}
// Call kstringTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "kstringTest::main") {
kstringTest::main();
}
?>
実行例
% phpunit kstringTest.php PHPUnit 2.3.4 by Sebastian Bergmann. ... Time: 0.003443 OK (3 tests)
例外処理のテストをする方法について例をあげる。
テストするコード kcrypt.php
<?php
class kcrypt
{
public function __construct ()
{
}
public function __destruct ()
{
}
private function isEmpty ($name, $value)
{
if ($value == '') {
throw new Exception ($name . ' is empty');
}
}
public function encrypt ($secret, $data)
{
$this->isEmpty ('secret', $secret);
$this->isEmpty ('data', $data);
$compute = 'fake...';
return ($compute);
}
public function decrypt ($secret, $data)
{
$this->isEmpty ('secret', $secret);
$this->isEmpty ('data', $data);
$decrypted = 'fake...';
return ($decrypted);
}
}
?>
phpunitコマンドを使って、kcrypt.phpからスケルトンを生成する。
% phpunit --skeleton kcrypt PHPUnit 2.3.4 by Sebastian Bergmann. Wrote test class skeleton for kcrypt to kcryptTest.php.
生成されたスケルトン kcryptTest.php
<?php
// Call kcryptTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
define("PHPUnit2_MAIN_METHOD", "kcryptTest::main");
}
require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";
// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";
require_once "kcrypt.php";
/**
* Test class for kcrypt.
* Generated by PHPUnit2_Util_Skeleton on 2007-08-24 at 00:31:03.
*/
class kcryptTest extends PHPUnit2_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once "PHPUnit2/TextUI/TestRunner.php";
$suite = new PHPUnit2_Framework_TestSuite("kcryptTest");
$result = PHPUnit2_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement test__destruct().
*/
public function test__destruct() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
/**
* @todo Implement testEncrypt().
*/
public function testEncrypt() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
/**
* @todo Implement testDecrypt().
*/
public function testDecrypt() {
// Remove the following line when you implement this test.
throw new PHPUnit2_Framework_IncompleteTestError;
}
}
// Call kcryptTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "kcryptTest::main") {
kcryptTest::main();
}
?>
スケルトン kcryptTest.php を書き換えた結果
<?php
// Call kcryptTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
define("PHPUnit2_MAIN_METHOD", "kcryptTest::main");
}
require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";
// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";
require_once "kcrypt.php";
/**
* Test class for kcrypt.
* Generated by PHPUnit2_Util_Skeleton on 2007-08-24 at 00:31:03.
*/
class kcryptTest extends PHPUnit2_Framework_TestCase {
/**
* Runs the test methods of this class.
*
* @access public
* @static
*/
public static function main() {
require_once "PHPUnit2/TextUI/TestRunner.php";
$suite = new PHPUnit2_Framework_TestSuite("kcryptTest");
$result = PHPUnit2_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @access protected
*/
protected function setUp() {
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @access protected
*/
protected function tearDown() {
}
/**
* @todo Implement test__destruct().
*/
public function test__destruct() {
}
/**
* @todo Implement testEncrypt().
*/
public function testEncrypt() {
$o = new kcrypt ();
try {
$compute = $o->encrypt ('', 'data');
$this->fail('Exception not thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$compute = $o->encrypt ('secret_data', '');
$this->fail('Exception not thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$compute = $o->encrypt ('secret_data', 'data');
$this->assertTrue(true);
} catch (Exception $e) {
$this->fail('Exception not thrown');
}
}
/**
* @todo Implement testDecrypt().
*/
public function testDecrypt() {
$o = new kcrypt ();
try {
$compute = $o->encrypt ('secret_data', 'data');
$this->assertTrue(true);
} catch (Exception $e) {
$this->fail('Exception not thrown');
}
try {
$decrypted = $o->decrypt ('', 'data', $compute);
$this->fail('Exception not thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$decrypted = $o->decrypt ('secret_data', '', $compute);
$this->fail('Exception not thrown');
} catch (Exception $e) {
$this->assertTrue(true);
}
try {
$decrypted = $o->decrypt ('secret_data', 'data', $compute);
$this->assertTrue(true);
} catch (Exception $e) {
$this->fail('Exception not thrown');
}
}
}
// Call kcryptTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "kcryptTest::main") {
kcryptTest::main();
}
?>
kcryptTest.phpの実行結果
% phpunit kcryptTest.php PHPUnit 2.3.4 by Sebastian Bergmann. ... Time: 0.006865 OK (3 tests)
1つ失敗をわざとさせてみたときの結果。
% phpunit kcryptTest.php PHPUnit 2.3.4 by Sebastian Bergmann. .F. Time: 0.006471 There was 1 failure: 1) testEncrypt(kcryptTest) Exception not thrown /home/kaworu/dev/php/phpunit/kcryptTest.php:80 FAILURES!!! Tests run: 3, Failures: 1, Errors: 0, Incomplete Tests: 0.
サンプル
下記ディレクトリに例があります。
/usr/local/share/examples/pear/PHPUnit2
スポンサーリンク
スポンサーリンク
いつもシェア、ありがとうございます!
もっと情報を探しませんか?
関連記事
最近の記事
- パナソニック ジェットウォッシャードルツ EW-DJ61-Wのホースの修理
- LinuxセキュリティモジュールIntegrity Policy Enforcement
- アマゾンのEcho Show 5を買ったのでレビューします
- アマゾンのサイバーマンデーはAlexa Echo Show 5が安い
- Android スマートフォン OnePlus 7T と OnePlus 7の違い
- Android スマートフォン OnePlus 7 をAndroid10にアップデートしてみた
- クレジットカードのバーチャルカードの比較のまとめ
- 活動量計 Xiaomi Mi Band 4を買ってみたのでレビュー
- Android スマートフォン OnePlus 7 のレビュー
- AliExpressでスマートフォンを買い物してみた
- パソコンのホコリ対策 レンジフードフィルターと養生テープ
- 80PLUS GOLDのPC電源ユニットAntec NeoEco 750 Goldのレビュー
- イギリスの付加価値税 VAT は払い戻しを受けられる
- イギリスのロンドンでスーツケースなど荷物を預けられる場所は
- イギリスのロンドンで地下鉄やバスに乗るならオイスターカードを使おう
- イギリスのヒースロー空港からロンドン市内への行き方
- 航空便でほかの航空会社に乗り継ぎがある場合のオンラインチェックイン
- SFC会員がANA便ではなくベトナム航空のコードシェアを試して解ったこと
- ベトナムの入国審査でeチケットの掲示が必要だった話
- シアトルの交通ICカードはオルカカード(Orca)です
人気のページ
- Windows7 IME 辞書ツールで単語の登録に失敗しました
- C言語 popen()でコマンドを実行して出力を読み込む
- Windows7で休止状態にする方法
- CentOS MySQLの起動、停止、再起動
- loggerコマンドでsyslogにエラーを出力する方法
- パソコンパーツの買取をしてくれる店のまとめ
- Java Mapの使い方 get(),put(),remove(),size(),clear()
- 楽天のRポイントカードを作ってみた
- iPhone 5 から iPhone 6 に乗り換えたのでレビュー
- netstatコマンドのステータスの意味
スポンサーリンク
過去ログ
2020 : 01 02 03 04 05 06 07 08 09 10 11 122019 : 01 02 03 04 05 06 07 08 09 10 11 12
2018 : 01 02 03 04 05 06 07 08 09 10 11 12
2017 : 01 02 03 04 05 06 07 08 09 10 11 12
2016 : 01 02 03 04 05 06 07 08 09 10 11 12
2015 : 01 02 03 04 05 06 07 08 09 10 11 12
2014 : 01 02 03 04 05 06 07 08 09 10 11 12
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
2003 : 01 02 03 04 05 06 07 08 09 10 11 12