スポンサーリンク

PHPのテストには、phpunitを使うことでユニットテストを作成し、ユニットテストを行うことができます。

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


スポンサーリンク
スポンサーリンク
 
いつもシェア、ありがとうございます!


もっと情報を探しませんか?

関連記事

最近の記事

人気のページ

スポンサーリンク
 

過去ログ

2020 : 01 02 03 04 05 06 07 08 09 10 11 12
2019 : 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

サイト

Vim入門

C言語入門

C++入門

JavaScript/Node.js入門

Python入門

FreeBSD入門

Ubuntu入門

セキュリティ入門

パソコン自作入門

ブログ

トップ


プライバシーポリシー