$30 off During Our Annual Pro Sale. View Details »

自作して理解するxUnit / self-made-xunit

自作して理解するxUnit / self-made-xunit

PHPカンファレンス北海道2019で発表したxUnitを自作することで理解するというトークです

Kazuki Higashiguchi

September 21, 2019
Tweet

More Decks by Kazuki Higashiguchi

Other Decks in Technology

Transcript

  1. © - BASE, Inc. X
    ⾃作して理解するxUnit
    . . PHP Conference Hokkaido
    - @hgsgtk

    View Slide

  2. © - BASE, Inc.
    : @hgsgtk
    Who am I?
    東⼝ 和暉 ( Higashiguchi Kazuki )
    Back-end Engineer (Go, PHP, Python )
    BASE BANK, Inc. (BASE, Inc.)
    / Dev Division / Tech Lead

    View Slide

  3. © - BASE, Inc.
    毎⽇仲良しテストコード

    View Slide

  4. © - BASE, Inc.
    ⾃分がPHPUnitの気持ちを知れば

    View Slide

  5. © - BASE, Inc.
    もっと仲良くなれる

    View Slide

  6. © - BASE, Inc.
    ⾃作して理解しよう

    View Slide

  7. © - BASE, Inc.
    ところでPHPUnitとは
    https://phpunit.de/

    View Slide

  8. © - BASE, Inc.
    xUnitとは
    テスティングフレームワークの総称
    Kent Beck⽒が作成したSmalltalk製の
    SUnitが親となり、Java‧PHP .etcへ
    https://en.wikipedia.org/wiki/XUnit

    View Slide

  9. © - BASE, Inc.
    xUnitファミリーが持っている機能
    • Test Method
    • Assertion Method
    • Test Suites
    • Run tests, Get report
    参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

    View Slide

  10. © - BASE, Inc.
    今⽇⾃作するもの
    • Test Method
    • Assertion Method
    • Test Suites
    • Run tests, Get report
    参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

    View Slide

  11. © - BASE, Inc.
    いざ

    View Slide

  12. © - BASE, Inc.
    hgsgtk/mpunit
    https://github.com/hgsgtk/mpunit

    View Slide

  13. © - BASE, Inc.
    こんな感じのテストを実⾏したい

    View Slide

  14. © - BASE, Inc.
    xUnitファミリーが持っている機能
    • Test Method
    • Assertion Method
    • Test Suites
    • Run tests, Get report
    参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

    View Slide

  15. © - BASE, Inc.
    まずは実⾏可能コマンドを作成
    /bin/mpunit

    View Slide

  16. © - BASE, Inc.
    Test Methodsをつくったら実⾏されるように
    .テストファイルを探す
    .テストクラスを探す
    .テストメソッドを探す
    .テストメソッドを実⾏する

    View Slide

  17. © - BASE, Inc.
    1. テストファイルを探す
    /src/Command.php
    *Test.php のファイルを集める

    View Slide

  18. © - BASE, Inc.
    2. テストクラスを探す
    /src/Command.php
    定義済みクラスから、ユーザー定義型で MPUnit\TestCaseを
    継承しているを探し出す

    View Slide

  19. © - BASE, Inc.
    (補⾜)クラスを集める
    • get_declared_classes()
    • 定義済みのクラスの名前を配列として返す
    • ReflectionClass::isUserDefined()
    • 内部クラスかどうかを検査
    • ReflectionClass::getParentClass()
    • 親クラスの情報を取得
    https://www.php.net/manual/ja/reflectionclass.isuserdefined.php
    https://www.php.net/manual/ja/function.get-declared-classes.php
    https://www.php.net/manual/ja/reflectionclass.getparentclass.php

    View Slide

  20. © - BASE, Inc.
    3. テストメソッドを探す
    /src/Command.php
    テストクラスのPUBLICメソッドから、先頭が test で始まるメ
    ソッドを探す

    View Slide

  21. © - BASE, Inc.
    (補⾜)関数を集める
    • ReflectionClass::getMethods
    • クラスのメソッド名を取得
    • ReflectionMethod::IS_PUBLIC etc
    https://www.php.net/manual/ja/reflectionclass.getmethods.php

    View Slide

  22. © - BASE, Inc.
    4. テストメソッドを実⾏する
    /src/Command.php
    実⾏、AssertionErrorがなければ成功(.)、あれば失敗(F)

    View Slide

  23. © - BASE, Inc.
    hgsgtk/mpunit
    https://github.com/hgsgtk/mpunit
    ⼀旦デモ 

    View Slide

  24. © - BASE, Inc.
    xUnitを知る
    • Test Methodの⾒つけ⽅の⼤きく2つ
    • Test Discovery‧Test Enumeration
    • 多くのxUnitファミリーは前者、フレー
    ムワークがテストメソッドを⾒つける、
    ユーザーが⼿動で登録しなくていい

    View Slide

  25. © - BASE, Inc.
    今⽇⾃作するもの
    • Test Method
    • Assertion Method
    • Test Suites
    • Run tests, Get report
    参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

    View Slide

  26. © - BASE, Inc.
    なにでアサーションするか
    • assert function(PHP)
    • (Minimumにやりたかったので)
    • beberlei/assert
    • webmozart/assert
    • hamcrest/hamcrest-php
    etc

    View Slide

  27. © - BASE, Inc.
    assert関連の設定をする
    /bin/mpunit

    View Slide

  28. © - BASE, Inc.
    (補⾜)assert⽤の設定ディレクティブ
    • zend.assertions (default: )
    • assert()を⽣成するか‧実⾏するか
    • 1 の場合、コードは⽣成され実⾏される
    • assert.exception (default: )
    • アサーション失敗に指定したオブジェクトをス
    ロー
    • 指定なしだとAssertionError
    https://www.php.net/manual/ja/function.assert.php
    https://www.php.net/manual/ja/class.assertionerror.php

    View Slide

  29. © - BASE, Inc.
    (補⾜)assert⽤のオプション
    • 様々なassertフラグを設定できる
    • ASSERT_ACTIVE
    • ASSERT_WARNING
    • ASSERT_BAIL
    • ASSERT_QUIET_EVAL
    • ASSERT_CALLBACK
    https://www.php.net/manual/ja/function.assert-options.php

    View Slide

  30. © - BASE, Inc.
    assertion
    assert関数を実⾏

    View Slide

  31. © - BASE, Inc.
    こんな感じのテストを実⾏したい
    TestCaseを継承したクラスから assertSame が使えるように

    View Slide

  32. © - BASE, Inc.
    実⾏結果

    View Slide

  33. © - BASE, Inc.
    まとめ
    • ⾃作すると、xUnitがどういうことをし
    てくれているか分かる(ex. Test
    Discovery)
    • テスティングフレームワークの提供者の
    気持ちに近づき、ユーザーの僕らのテス
    トリテラシーも上がるのかもしれない

    View Slide

  34. © - BASE, Inc.
    hours
    制作時間

    View Slide

  35. © - BASE, Inc.
    Special Thanks: 参考資料
    • PHPでテスティングフレームワークを実装する前
    に知っておきたい勘所 / @tadsan
    • https://www.youtube.com/
    watch?v=o YY JnAOwU
    • sebastianbergmann/phpunit
    • https://github.com/sebastianbergmann/
    phpunit

    View Slide

  36. © - BASE, Inc. X
    みなさんもぜひ

    View Slide