PHPUnit

XAMPPの場合

PEARのアップグレード

  1. C:\Tools\Works\pleiades\xampp\php>pear update-channels
  2. Updating channel "pear.php.net"
  3. Update of Channel "pear.php.net" succeeded
  4. Updating channel "pecl.php.net"
  5. Update of Channel "pecl.php.net" succeeded
  6.  
  7. C:\Tools\Works\pleiades\xampp\php>
  8.  
  9.  
  10. C:\Tools\Works\pleiades\xampp\php>pear upgrade-all
  11.  
  12. C:\Tools\Works\pleiades\xampp\php>
  13.  
  14.  

古いPHPUnitのアンインストール

  1. C:\Tools\Works\pleiades\xampp\php>pear list
  2. INSTALLED PACKAGES, CHANNEL PEAR.PHP.NET:
  3. =========================================
  4. PACKAGE VERSION STATE
  5. PHPUnit 1.3.2 stable
  6. PHPUnit2 2.3.6 stable
  7.  
  8. C:\Tools\Works\pleiades\xampp\php>
  9.  
  10. C:\Tools\Works\pleiades\xampp\php>pear uninstall phpunit
  11. uninstall ok: channel://pear.php.net/PHPUnit-1.3.2
  12.  
  13. C:\Tools\Works\pleiades\xampp\php>
  14.  
  15. C:\Tools\Works\pleiades\xampp\php>pear uninstall phpunit2
  16. uninstall ok: channel://pear.php.net/PHPUnit2-2.3.6
  17.  
  18. C:\Tools\Works\pleiades\xampp\php>
  19.  
  20.  

インストール

  1. C:\Tools\Works\pleiades\xampp\php>pear channel-discover pear.phpunit.de
  2. Adding Channel "pear.phpunit.de" succeeded
  3. Discovery of channel "pear.phpunit.de" succeeded
  4.  
  5. C:\Tools\Works\pleiades\xampp\php>pear channel-discover components.ez.no
  6. Adding Channel "components.ez.no" succeeded
  7. Discovery of channel "components.ez.no" succeeded
  8.  
  9. C:\Tools\Works\pleiades\xampp\php>pear channel-discover pear.symfony-project.com
  10. Adding Channel "pear.symfony-project.com" succeeded
  11. Discovery of channel "pear.symfony-project.com" succeeded
  12.  
  13.  
  14. C:\Tools\Works\pleiades\xampp\php>pear install phpunit/PHPUnit
  15. Did not download optional dependencies: phpunit/PHP_Invoker, use --alldeps to download automatically
  16. phpunit/PHPUnit can optionally use package "phpunit/PHP_Invoker" (version >= 1.0.0)
  17. downloading PHPUnit-3.6.3.tgz ...
  18. Starting to download PHPUnit-3.6.3.tgz (116,362 bytes)
  19. .........................done: 116,362 bytes
  20. downloading File_Iterator-1.3.0.tgz ...
  21. Starting to download File_Iterator-1.3.0.tgz (5,109 bytes)
  22. ...done: 5,109 bytes
  23. downloading Text_Template-1.1.1.tgz ...
  24. Starting to download Text_Template-1.1.1.tgz (3,622 bytes)
  25. ...done: 3,622 bytes
  26. downloading PHP_CodeCoverage-1.1.1.tgz ...
  27. Starting to download PHP_CodeCoverage-1.1.1.tgz (132,339 bytes)
  28. ...done: 132,339 bytes
  29. downloading PHP_Timer-1.0.2.tgz ...
  30. Starting to download PHP_Timer-1.0.2.tgz (3,686 bytes)
  31. ...done: 3,686 bytes
  32. downloading PHPUnit_MockObject-1.1.0.tgz ...
  33. Starting to download PHPUnit_MockObject-1.1.0.tgz (19,688 bytes)
  34. ...done: 19,688 bytes
  35. downloading YAML-1.0.6.tgz ...
  36. Starting to download YAML-1.0.6.tgz (10,010 bytes)
  37. ...done: 10,010 bytes
  38. downloading PHP_TokenStream-1.1.1.tgz ...
  39. Starting to download PHP_TokenStream-1.1.1.tgz (9,672 bytes)
  40. ...done: 9,672 bytes
  41. install ok: channel://pear.phpunit.de/File_Iterator-1.3.0
  42. install ok: channel://pear.phpunit.de/Text_Template-1.1.1
  43. install ok: channel://pear.phpunit.de/PHP_Timer-1.0.2
  44. install ok: channel://pear.symfony-project.com/YAML-1.0.6
  45. install ok: channel://pear.phpunit.de/PHP_TokenStream-1.1.1
  46. install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.1
  47. install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.0
  48. install ok: channel://pear.phpunit.de/PHPUnit-3.6.3
  49.  
  50. C:\Tools\Works\pleiades\xampp\php>
  51.  
  52. C:\Tools\Works\pleiades\xampp\php>phpunit --version
  53. PHPUnit 3.6.3 by Sebastian Bergmann.
  54.  
  55.  
  56. C:\Tools\Works\pleiades\xampp\php>
  57.  
  58.  

テストサンプル

クラスコード

  1. <?php
  2. /**
  3.  * Generated by PHPUnit on 2009-10-22 at 13:07:33.
  4.  */
  5. class Employee
  6. {
  7. protected $age, $name;
  8.  
  9. /**
  10.   * @todo Implement getAge().
  11.   */
  12. public function getAge()
  13. {
  14. return $this->age;
  15. // Remove the following line when you implement this method.
  16. throw new RuntimeException('Not yet implemented.');
  17. }
  18.  
  19. /**
  20.   * @todo Implement getName().
  21.   */
  22. public function getName()
  23. {
  24. return $this->name;
  25. // Remove the following line when you implement this method.
  26. throw new RuntimeException('Not yet implemented.');
  27. }
  28.  
  29. /**
  30.   * @todo Implement setAge().
  31.   */
  32. public function setAge($age)
  33. {
  34. $this->age = $age;
  35. // Remove the following line when you implement this method.
  36. //throw new RuntimeException('Not yet implemented.');
  37. }
  38.  
  39. /**
  40.   * @todo Implement setName().
  41.   */
  42. public function setName($name)
  43. {
  44. $this->name = $name;
  45. // Remove the following line when you implement this method.
  46. //throw new RuntimeException('Not yet implemented.');
  47. }
  48. }?>
  49.  

テストコード

  1. <?php
  2.  
  3. require_once 'C:\Tools\Works\php\sample01\Employee.php';
  4.  
  5. /**
  6.  * Test class for Employee.
  7.  * Generated by PHPUnit on 2011-11-23 at 07:15:11.
  8.  */
  9. class EmployeeTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12.   * @var Employee
  13.   */
  14. protected $object;
  15.  
  16. /**
  17.   * Sets up the fixture, for example, opens a network connection.
  18.   * This method is called before a test is executed.
  19.   */
  20. protected function setUp()
  21. {
  22. $this->object = new Employee;
  23. }
  24.  
  25. /**
  26.   * Tears down the fixture, for example, closes a network connection.
  27.   * This method is called after a test is executed.
  28.   */
  29. protected function tearDown()
  30. {
  31. }
  32.  
  33. /**
  34.   * @covers {className}::{origMethodName}
  35.   * @todo Implement testGetAge().
  36.   */
  37. public function testGetAge()
  38. {
  39. // Remove the following lines when you implement this test.
  40. $this->markTestIncomplete(
  41. 'This test has not been implemented yet.'
  42. );
  43. }
  44.  
  45. /**
  46.   * @covers {className}::{origMethodName}
  47.   * @todo Implement testGetName().
  48.   */
  49. public function testGetName()
  50. {
  51. // Remove the following lines when you implement this test.
  52. $this->markTestIncomplete(
  53. 'This test has not been implemented yet.'
  54. );
  55. }
  56.  
  57. /**
  58.   * @covers {className}::{origMethodName}
  59.   * @todo Implement testSetAge().
  60.   */
  61. public function testSetAge()
  62. {
  63. // Remove the following lines when you implement this test.
  64. $this->markTestIncomplete(
  65. 'This test has not been implemented yet.'
  66. );
  67. }
  68.  
  69. /**
  70.   * @covers {className}::{origMethodName}
  71.   * @todo Implement testSetName().
  72.   */
  73. public function testSetName()
  74. {
  75. // Remove the following lines when you implement this test.
  76. $this->markTestIncomplete(
  77. 'This test has not been implemented yet.'
  78. );
  79. }
  80. }
  81. ?>
  82.  

テスト

  1. C:\Tools\Works\php\sample01>phpunit EmployeeTest.php
  2. PHPUnit 3.6.3 by Sebastian Bergmann.
  3.  
  4. IIII
  5.  
  6. Time: 1 second, Memory: 3.25Mb
  7.  
  8. OK, but incomplete or skipped tests!
  9. Tests: 4, Assertions: 0, Incomplete: 4.
  10.  
  11. C:\Tools\Works\php\sample01>
  12.  

使用メソッド

メソッド名 呼び出しタイミング
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()

雛形ジェネレータ

クラスファイルからテストコードの雛形を作成

  1. C:\Tools\Works\php\sample01>phpunit --skeleton-test Employee
  2. PHPUnit 3.6.3 by Sebastian Bergmann.
  3.  
  4. Wrote skeleton for "EmployeeTest" to "C:\Tools\Works\php\sample01\EmployeeTest.php".
  5.  
  6. C:\Tools\Works\php\sample01>
  7.  

カバレッジ

テスト結果より、HTML形式のカバレッジを作成

  1. C:\Tools\Works\php\sample01>phpunit --coverage-html ./report EmployeeTest
  2. PHPUnit 3.6.3 by Sebastian Bergmann.
  3.  
  4. IIII
  5.  
  6. Time: 1 second, Memory: 3.75Mb
  7.  
  8. OK, but incomplete or skipped tests!
  9. Tests: 4, Assertions: 0, Incomplete: 4.
  10.  
  11. Generating code coverage report, this may take a moment.
  12.  
  13. C:\Tools\Works\php\sample01>
  14.  

拡張機能

Seleniumをインストール

php.iniより"extension=php_curl.dll"をコメントされている場合は解除する

pearでインストールを行う

  1. C:\Tools\Works>pear upgrade phpunit/PHPUnit_Selenium
  2. Package "pear.phpunit.de/PHPUnit_Selenium" dependency "pear.phpunit.de/PHPUnit"
  3. downloading PHPUnit_Selenium-1.1.0.tgz ...
  4. Starting to download PHPUnit_Selenium-1.1.0.tgz (16,318 bytes)
  5. ......done: 16,318 bytes
  6. upgrade ok: channel://pear.phpunit.de/PHPUnit_Selenium-1.1.0
  7.  
  8. C:\Tools\Works>
  9.  
  10.  

DBUnitをインストール

pearでインストールを行う

  1. C:\Tools\Works>pear install phpunit/DbUnit
  2. Package "pear.phpunit.de/DbUnit" dependency "pear.phpunit.de/PHPUnit" has no releases
  3. Package "pear.phpunit.de/DbUnit" dependency "pear.symfony-project.com/YAML" has no releases
  4. downloading DbUnit-1.1.1.tgz ...
  5. Starting to download DbUnit-1.1.1.tgz (41,906 bytes)
  6. ............done: 41,906 bytes
  7. install ok: channel://pear.phpunit.de/DbUnit-1.1.1
  8.  
  9. C:\Tools\Works>
  10.  
  11.  


最終更新:2011年11月29日 06:20