getting started with testing using phpunit

40
PHPUnit で テストを始めよう Piece Project 株式会社アイテマン 久保敦啓 (KUBO Atsuhiro) 2011/10/15 PHP Matsuri 2011 Spiral Staircase By Christopher Blizzard http://www.flickr.com/photos/christopherblizzard/306043084

Upload: atsuhiro-kubo

Post on 28-May-2015

2.112 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Getting Started with Testing using PHPUnit

PHPUnit でテストを始めよう

Piece Project株式会社アイテマン久保敦啓 (KUBO Atsuhiro)

2011/10/15 PHP Matsuri 2011

Spiral Staircase By Christopher Blizzardhttp://www.flickr.com/photos/christopherblizzard/306043084

Page 2: Getting Started with Testing using PHPUnit

久保敦啓 (KUBO Atsuhiro) Piece Project プロジェクトリード 株式会社アイテマン 代表取締役 Twitter @iteman メンタリング

師匠承ります ソフトウェア開発

Page 3: Getting Started with Testing using PHPUnit

Piece Framework by Piece Project PHP のアプリケーションフレームワーク オープンソースソフトウェア Piece Project

開発者募集中 スポンサー募集中

Page 4: Getting Started with Testing using PHPUnit

アジェンダ テスト? テストの自動化 PHPUnit の導入 Stagehand_TestRunner の導入 MakeGood の導入

Page 5: Getting Started with Testing using PHPUnit

test By DaveBleasdalehttp://www.flickr.com/photos/sidelong/246816211

テスト?

Page 6: Getting Started with Testing using PHPUnit

さまざまなテストの分類方法 テストレベル

単体テスト、結合テスト、システムテスト、受け入れテスト

テストタイプ機能テスト、非機能テスト、構造テスト、回帰テスト

テスト設計技法構造ベース、仕様ベース、経験ベース

Page 7: Getting Started with Testing using PHPUnit

Questions By Oberazzihttp://www.flickr.com/photos/oberazzi/318947873

Page 8: Getting Started with Testing using PHPUnit

誰が何のために?

Page 9: Getting Started with Testing using PHPUnit

3 種類のテスト

Page 10: Getting Started with Testing using PHPUnit

Developer Testing 誰が?

開発者が 何のために?

開発促進のために

Page 11: Getting Started with Testing using PHPUnit

QA Testing 誰が?

品質保証担当者が 何のために?

品質保証のために QA = Quality Assurance

Page 12: Getting Started with Testing using PHPUnit

Customer Testing 誰が?

顧客が 何のために?

進捗管理のために

Page 13: Getting Started with Testing using PHPUnit

開発プロセス・方法論 テスト駆動開発 (TDD)

Test-Driven Development 受け入れテスト駆動開発 (ATDD) 検証指向 TDD

ビヘイビア駆動開発 (BDD)

Behavior-Driven Development

Page 14: Getting Started with Testing using PHPUnit

テスト駆動開発

Page 15: Getting Started with Testing using PHPUnit

3 種類のテストと開発手法

Page 16: Getting Started with Testing using PHPUnit

テストを実行する?

Page 17: Getting Started with Testing using PHPUnit

テストの実行プロセス1.テストを実行する。

2.期待する結果と実際の結果を比較する。

3.結果をリポートする。

Page 18: Getting Started with Testing using PHPUnit

手作業で?

Galley Slaves By ChibiJoshhttp://www.flickr.com/photos/chibijosh/17867308

Page 19: Getting Started with Testing using PHPUnit

Escalators By HKmPUAhttp://www.flickr.com/photos/hleung/1295585574

テストの自動化

Page 20: Getting Started with Testing using PHPUnit

テスティングフレームワーク PHPUnit Behat PHPSpec …

Page 21: Getting Started with Testing using PHPUnit

PHPUnitclass StackTest extends PHPUnit_Framework_TestCase{ public function testPushAndPop() { $stack = array(); $this->assertEquals(0, count($stack)); array_push($stack, 'foo'); $this->assertEquals('foo', $stack[count($stack)-1]); $this->assertEquals(1, count($stack)); $this->assertEquals('foo', array_pop($stack)); $this->assertEquals(0, count($stack)); }

Page 22: Getting Started with Testing using PHPUnit

BehatFeature: ls In order to see the directory structure As a UNIX user I need to be able to list the current directory's contents

Scenario: Given I am in a directory "test" And I have a file named "foo" And I have a file named "bar" When I run "ls" Then I should get: """ bar foo """

Page 23: Getting Started with Testing using PHPUnit

PHPSpecclass DescribeNewBowlingGame extends \PHPSpec\Context{ private $_bowling = null; public function before() { $this->_bowling = $this->spec(new Bowling); } public function itShouldScore0ForGutterGame() { for ($i=1; $i<=20; $i++) { // someone is really bad at bowling! $this->_bowling->hit(0); } $this->_bowling->score->should->equal(0); }

Page 24: Getting Started with Testing using PHPUnit

3 種類のテストとフレームワーク

Page 25: Getting Started with Testing using PHPUnit

さらなる自動化

Page 26: Getting Started with Testing using PHPUnit

継続的テスト

CCTV By alancleaver_2000http://www.flickr.com/photos/alancleaver/4103405016

Page 27: Getting Started with Testing using PHPUnit

継続的テストツール MakeGood Stagehand_TestRunner

Page 28: Getting Started with Testing using PHPUnit

継続的インテグレーション

Monitor By Boyce Dupreyhttp://www.flickr.com/photos/boyce-d/5160787240

Page 29: Getting Started with Testing using PHPUnit

継続的インテグレーションサーバ Jenkins TeamCity CruiseControl … Sismo (created by Fabien Potencier)

Page 30: Getting Started with Testing using PHPUnit

PHP Tester's Toolbox : Sebastian Bergmann By Stuart Herberthttp://www.flickr.com/photos/stuartherbert/6231499431

PHPUnit の導入

Page 31: Getting Started with Testing using PHPUnit

インストール$ pear channel-discover components.ez.no$ pear channel-discover pear.symfony-project.com$ pear channel-discover pear.phpunit.de$ pear install phpunit/phpunit-3.5.15

Page 32: Getting Started with Testing using PHPUnit

テストの作成FooTest.php:

<?phpclass FooTest extends PHPUnit_Framework_TestCase{ /** * @test */ public function alwaysTrue() { $expected = $actual = true; $this->assertEquals($expected, $actual); }}

Page 33: Getting Started with Testing using PHPUnit

テストの実行$ phpunit FooTest.php PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 6.00Mb

OK (1 test, 1 assertion)

Page 34: Getting Started with Testing using PHPUnit

Stagehand_TestRunner の導入

Page 35: Getting Started with Testing using PHPUnit

インストール$ pear channel-discover pear.piece-framework.com$ pear install piece/stagehand_testrunner

Page 36: Getting Started with Testing using PHPUnit

テストの実行$ phpunitrunner FooTest.php PHPUnit 3.5.15 by Sebastian Bergmann.

.

Foo [x] Always true

Time: 0 seconds, Memory: 6.75Mb

OK (1 test, 1 assertion)

Page 37: Getting Started with Testing using PHPUnit

継続的テスト$ phpunitrunner -a .(FooTest.php を編集するとテストが実行される。 )$ phpunitrunner -an .(Growl/notify-send で結果が通知される。 )

Page 38: Getting Started with Testing using PHPUnit

MakeGood の導入

Page 39: Getting Started with Testing using PHPUnit

和田卓人「[動画で解説]和田卓人の“テスト駆動開発”講座:第 3 回 「テスト」という言葉について ── Developer Testing , Customer Testing , QA Testing 」

http://gihyo.jp/dev/serial/01/tdd/0003, 2007 年。

川西俊之「 DevLOVE LT: Do you know axes of software testing? 」

http://www.slideshare.net/tosikawa/ss-1632677, 2009 年。

大田健一郎「テストエンジニアと TDD 」『ソフトウェア・テスト PRESS 総集編』、技術評論社、 2011 年。

参考

Page 40: Getting Started with Testing using PHPUnit

Sebastian Bergmann 「 PHPUnit Manual 」

http://www.phpunit.de/manual/3.5/ja/index.html

Konstantin Kudryashov 「 Behat Documentation 」

http://docs.behat.org/index.html

Pádraic Brady, The PHPSpec Development Team 「 Reference Manual 」

http://www.phpspec.net/documentation/

参考