PHPUnit
XAMPPの場合
PEARのアップグレード
C:\Tools\Works\pleiades\xampp\php>pear update-channels
Updating channel "pear.php.net"
Update of Channel "pear.php.net" succeeded
Updating channel "pecl.php.net"
Update of Channel "pecl.php.net" succeeded
C:\Tools\Works\pleiades\xampp\php>
C:\Tools\Works\pleiades\xampp\php>pear upgrade-all
C:\Tools\Works\pleiades\xampp\php>
古いPHPUnitのアンインストール
C:\Tools\Works\pleiades\xampp\php>pear list
INSTALLED PACKAGES, CHANNEL PEAR.PHP.NET:
=========================================
PACKAGE VERSION STATE
PHPUnit 1.3.2 stable
PHPUnit2 2.3.6 stable
C:\Tools\Works\pleiades\xampp\php>
C:\Tools\Works\pleiades\xampp\php>pear uninstall phpunit
uninstall ok: channel://pear.php.net/PHPUnit-1.3.2
C:\Tools\Works\pleiades\xampp\php>
C:\Tools\Works\pleiades\xampp\php>pear uninstall phpunit2
uninstall ok: channel://pear.php.net/PHPUnit2-2.3.6
C:\Tools\Works\pleiades\xampp\php>
インストール
C:\Tools\Works\pleiades\xampp\php>pear channel-discover pear.phpunit.de
Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded
C:\Tools\Works\pleiades\xampp\php>pear channel-discover components.ez.no
Adding Channel "components.ez.no" succeeded
Discovery of channel "components.ez.no" succeeded
C:\Tools\Works\pleiades\xampp\php>pear channel-discover pear.symfony-project.com
Adding Channel "pear.symfony-project.com" succeeded
Discovery of channel "pear.symfony-project.com" succeeded
C:\Tools\Works\pleiades\xampp\php>pear install phpunit/PHPUnit
Did not download optional dependencies: phpunit/PHP_Invoker, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package "phpunit/PHP_Invoker" (version >= 1.0.0)
downloading PHPUnit-3.6.3.tgz ...
Starting to download PHPUnit-3.6.3.tgz (116,362 bytes)
.........................done: 116,362 bytes
downloading File_Iterator-1.3.0.tgz ...
Starting to download File_Iterator-1.3.0.tgz (5,109 bytes)
...done: 5,109 bytes
downloading Text_Template-1.1.1.tgz ...
Starting to download Text_Template-1.1.1.tgz (3,622 bytes)
...done: 3,622 bytes
downloading PHP_CodeCoverage-1.1.1.tgz ...
Starting to download PHP_CodeCoverage-1.1.1.tgz (132,339 bytes)
...done: 132,339 bytes
downloading PHP_Timer-1.0.2.tgz ...
Starting to download PHP_Timer-1.0.2.tgz (3,686 bytes)
...done: 3,686 bytes
downloading PHPUnit_MockObject-1.1.0.tgz ...
Starting to download PHPUnit_MockObject-1.1.0.tgz (19,688 bytes)
...done: 19,688 bytes
downloading YAML-1.0.6.tgz ...
Starting to download YAML-1.0.6.tgz (10,010 bytes)
...done: 10,010 bytes
downloading PHP_TokenStream-1.1.1.tgz ...
Starting to download PHP_TokenStream-1.1.1.tgz (9,672 bytes)
...done: 9,672 bytes
install ok: channel://pear.phpunit.de/File_Iterator-1.3.0
install ok: channel://pear.phpunit.de/Text_Template-1.1.1
install ok: channel://pear.phpunit.de/PHP_Timer-1.0.2
install ok: channel://pear.symfony-project.com/YAML-1.0.6
install ok: channel://pear.phpunit.de/PHP_TokenStream-1.1.1
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.1
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.0
install ok: channel://pear.phpunit.de/PHPUnit-3.6.3
C:\Tools\Works\pleiades\xampp\php>
C:\Tools\Works\pleiades\xampp\php>phpunit --version
PHPUnit 3.6.3 by Sebastian Bergmann.
C:\Tools\Works\pleiades\xampp\php>
テストサンプル
クラスコード
<?php
/**
* Generated by PHPUnit on 2009-10-22 at 13:07:33.
*/
class Employee
{
protected $age, $name;
/**
* @todo Implement getAge().
*/
public function getAge()
{
return $this->age;
// Remove the following line when you implement this method.
throw new RuntimeException('Not yet implemented.');
}
/**
* @todo Implement getName().
*/
public function getName()
{
return $this->name;
// Remove the following line when you implement this method.
throw new RuntimeException('Not yet implemented.');
}
/**
* @todo Implement setAge().
*/
public function setAge($age)
{
$this->age = $age;
// Remove the following line when you implement this method.
//throw new RuntimeException('Not yet implemented.');
}
/**
* @todo Implement setName().
*/
public function setName($name)
{
$this->name = $name;
// Remove the following line when you implement this method.
//throw new RuntimeException('Not yet implemented.');
}
}?>
テストコード
<?php
require_once 'C:\Tools\Works\php\sample01\Employee.php';
/**
* Test class for Employee.
* Generated by PHPUnit on 2011-11-23 at 07:15:11.
*/
class EmployeeTest extends PHPUnit_Framework_TestCase
{
/**
* @var Employee
*/
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 Employee;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGetAge().
*/
public function testGetAge()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testGetName().
*/
public function testGetName()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testSetAge().
*/
public function testSetAge()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers {className}::{origMethodName}
* @todo Implement testSetName().
*/
public function testSetName()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
?>
テスト
C:\Tools\Works\php\sample01>phpunit EmployeeTest.php
PHPUnit 3.6.3 by Sebastian Bergmann.
IIII
Time: 1 second, Memory: 3.25Mb
OK, but incomplete or skipped tests!
Tests: 4, Assertions: 0, Incomplete: 4.
C:\Tools\Works\php\sample01>
使用メソッド
メソッド名 |
呼び出しタイミング |
setUpBeforeClass() |
テストケース開始時 |
setUp() |
テストメソッド開始時 |
tearDown() |
テストメソッド終了時 |
tearDownAfterClass() |
テストケース終了時 |
アサーションメソッド
メソッド名 |
チェック内容 |
assertArrayHasKey() |
|
assertClassHasAttribute() |
|
assertClassHasStaticAttribute() |
|
assertContains() |
|
assertContainsOnly() |
|
assertEmpty() |
|
assertEqualXMLStructure() |
|
assertEquals() |
|
assertFalse() |
|
assertFileEquals() |
|
assertFileExists() |
|
assertGreaterThan() |
|
assertGreaterThanOrEqual() |
|
assertInstanceOf() |
|
assertInternalType() |
|
assertLessThan() |
|
assertLessThanOrEqual() |
|
assertNull() |
|
assertObjectHasAttribute() |
|
assertRegExp() |
|
assertStringMatchesFormat() |
|
assertStringMatchesFormatFile() |
|
assertSame() |
|
assertSelectCount() |
|
assertSelectEquals() |
|
assertSelectRegExp() |
|
assertStringEndsWith() |
|
assertStringEqualsFile() |
|
assertStringStartsWith() |
|
assertTag() |
|
assertThat() |
|
assertTrue() |
|
assertType() |
|
assertXmlFileEqualsXmlFile() |
|
assertXmlStringEqualsXmlFile() |
|
assertXmlStringEqualsXmlString() |
|
雛形ジェネレータ
クラスファイルからテストコードの雛形を作成
C:\Tools\Works\php\sample01>phpunit --skeleton-test Employee
PHPUnit 3.6.3 by Sebastian Bergmann.
Wrote skeleton for "EmployeeTest" to "C:\Tools\Works\php\sample01\EmployeeTest.php".
C:\Tools\Works\php\sample01>
カバレッジ
テスト結果より、HTML形式のカバレッジを作成
C:\Tools\Works\php\sample01>phpunit --coverage-html ./report EmployeeTest
PHPUnit 3.6.3 by Sebastian Bergmann.
IIII
Time: 1 second, Memory: 3.75Mb
OK, but incomplete or skipped tests!
Tests: 4, Assertions: 0, Incomplete: 4.
Generating code coverage report, this may take a moment.
C:\Tools\Works\php\sample01>
拡張機能
Seleniumをインストール
php.iniより"extension=php_curl.dll"をコメントされている場合は解除する
pearでインストールを行う
C:\Tools\Works>pear upgrade phpunit/PHPUnit_Selenium
Package "pear.phpunit.de/PHPUnit_Selenium" dependency "pear.phpunit.de/PHPUnit"
downloading PHPUnit_Selenium-1.1.0.tgz ...
Starting to download PHPUnit_Selenium-1.1.0.tgz (16,318 bytes)
......done: 16,318 bytes
upgrade ok: channel://pear.phpunit.de/PHPUnit_Selenium-1.1.0
C:\Tools\Works>
DBUnitをインストール
pearでインストールを行う
C:\Tools\Works>pear install phpunit/DbUnit
Package "pear.phpunit.de/DbUnit" dependency "pear.phpunit.de/PHPUnit" has no releases
Package "pear.phpunit.de/DbUnit" dependency "pear.symfony-project.com/YAML" has no releases
downloading DbUnit-1.1.1.tgz ...
Starting to download DbUnit-1.1.1.tgz (41,906 bytes)
............done: 41,906 bytes
install ok: channel://pear.phpunit.de/DbUnit-1.1.1
C:\Tools\Works>
最終更新:2011年11月29日 06:20