2011/10/15 PHP Matsuri 2011




                PHPUnit で
               テストを始めよう
                                                         Piece Project
                                                         株式会社アイテマン
Spiral Staircase By Christopher Blizzard                 久保敦啓 (KUBO Atsuhiro)
http://www.flickr.com/photos/christopherblizzard/306043084
久保敦啓 (KUBO Atsuhiro)

    Piece Project プロジェクトリード

    株式会社アイテマン 代表取締役

    Twitter @iteman

    メンタリング
      
          師匠承ります

    ソフトウェア開発
Piece Framework by Piece Project

    PHP のアプリケーションフレームワーク

    オープンソースソフトウェア

    Piece Project
      
          開発者募集中
      
          スポンサー募集中
アジェンダ

    テスト?

    テストの自動化

    PHPUnit の導入

    Stagehand_TestRunner の導入

    MakeGood の導入
テスト?


   test By DaveBleasdale
   http://www.flickr.com/photos/sidelong/246816211
さまざまなテストの分類方法

    テストレベル
    単体テスト、結合テスト、システムテスト、受け入れテスト

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

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


    …
Questions By Oberazzi
http://www.flickr.com/photos/oberazzi/318947873
誰が何のために?
3 種類のテスト
Developer Testing

    誰が?
     
         開発者が

    何のために?
     
         開発促進のために
QA Testing

    誰が?
      
          品質保証担当者が

    何のために?
      
          品質保証のために

    QA = Quality Assurance
Customer Testing

    誰が?
     
         顧客が

    何のために?
     
         進捗管理のために
開発プロセス・方法論

    テスト駆動開発 (TDD)
    Test-Driven Development
      
          受け入れテスト駆動開発 (ATDD)
      
          検証指向 TDD

    ビヘイビア駆動開発 (BDD)
    Behavior-Driven Development
テスト駆動開発
3 種類のテストと開発手法
テストを実行する?
テストの実行プロセス
1.テストを実行する。
2.期待する結果と実際の結果を比較する。
3.結果をリポートする。
手作業で?


   Galley Slaves By ChibiJosh
   http://www.flickr.com/photos/chibijosh/17867308
テストの自動化


    Escalators By HKmPUA
    http://www.flickr.com/photos/hleung/1295585574
テスティングフレームワーク

    PHPUnit

    Behat

    PHPSpec

    …
PHPUnit
class 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));
   }
Behat
Feature: 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
       """
PHPSpec
class DescribeNewBowlingGame extends PHPSpecContext
{
    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);
    }
3 種類のテストとフレームワーク
さらなる自動化
継続的テスト



CCTV By alancleaver_2000
http://www.flickr.com/photos/alancleaver/4103405016
継続的テストツール

    MakeGood

    Stagehand_TestRunner
継続的インテグレーション



       Monitor By Boyce Duprey
       http://www.flickr.com/photos/boyce-d/5160787240
継続的インテグレーションサーバ

    Jenkins

    TeamCity

    CruiseControl

    …

    Sismo (created by Fabien Potencier)
PHPUnit の導入




PHP Tester's Toolbox : Sebastian Bergmann By Stuart Herbert
http://www.flickr.com/photos/stuartherbert/6231499431
インストール
$   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
テストの作成
FooTest.php:

<?php
class FooTest extends PHPUnit_Framework_TestCase
{
    /**
      * @test
      */
    public function alwaysTrue()
    {
         $expected = $actual = true;
         $this->assertEquals($expected, $actual);
    }
}
テストの実行
$ phpunit FooTest.php
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 6.00Mb

OK (1 test, 1 assertion)
Stagehand_TestRunner の導入
インストール
$ pear channel-discover pear.piece-framework.com
$ pear install piece/stagehand_testrunner
テストの実行
$ 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)
継続的テスト
$ phpunitrunner -a .
(FooTest.php を編集するとテストが実行される。 )
$ phpunitrunner -an .
(Growl/notify-send で結果が通知される。 )
MakeGood の導入
参考

    和田卓人「[動画で解説]和田卓人の“テスト駆動開発”講座:第 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 年。
参考

    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/

Getting Started with Testing using PHPUnit