PHPカンファレンス北海道2019で発表したxUnitを自作することで理解するというトークです
© - BASE, Inc. X⾃作して理解するxUnit. . PHP Conference Hokkaido- @hgsgtk
View Slide
© - BASE, Inc.: @hgsgtkWho am I?東⼝ 和暉 ( Higashiguchi Kazuki )Back-end Engineer (Go, PHP, Python )BASE BANK, Inc. (BASE, Inc.)/ Dev Division / Tech Lead
© - BASE, Inc.毎⽇仲良しテストコード
© - BASE, Inc.⾃分がPHPUnitの気持ちを知れば
© - BASE, Inc.もっと仲良くなれる
© - BASE, Inc.⾃作して理解しよう
© - BASE, Inc.ところでPHPUnitとはhttps://phpunit.de/
© - BASE, Inc.xUnitとはテスティングフレームワークの総称Kent Beck⽒が作成したSmalltalk製のSUnitが親となり、Java‧PHP .etcへhttps://en.wikipedia.org/wiki/XUnit
© - BASE, Inc.xUnitファミリーが持っている機能• Test Method• Assertion Method• Test Suites• Run tests, Get report参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
© - BASE, Inc.今⽇⾃作するもの• Test Method• Assertion Method• Test Suites• Run tests, Get report参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
© - BASE, Inc.いざ
© - BASE, Inc.hgsgtk/mpunithttps://github.com/hgsgtk/mpunit
© - BASE, Inc.こんな感じのテストを実⾏したい
© - BASE, Inc.まずは実⾏可能コマンドを作成/bin/mpunit
© - BASE, Inc.Test Methodsをつくったら実⾏されるように.テストファイルを探す.テストクラスを探す.テストメソッドを探す.テストメソッドを実⾏する
© - BASE, Inc.1. テストファイルを探す/src/Command.php*Test.php のファイルを集める
© - BASE, Inc.2. テストクラスを探す/src/Command.php定義済みクラスから、ユーザー定義型で MPUnit\TestCaseを継承しているを探し出す
© - BASE, Inc.(補⾜)クラスを集める• get_declared_classes()• 定義済みのクラスの名前を配列として返す• ReflectionClass::isUserDefined()• 内部クラスかどうかを検査• ReflectionClass::getParentClass()• 親クラスの情報を取得https://www.php.net/manual/ja/reflectionclass.isuserdefined.phphttps://www.php.net/manual/ja/function.get-declared-classes.phphttps://www.php.net/manual/ja/reflectionclass.getparentclass.php
© - BASE, Inc.3. テストメソッドを探す/src/Command.phpテストクラスのPUBLICメソッドから、先頭が test で始まるメソッドを探す
© - BASE, Inc.(補⾜)関数を集める• ReflectionClass::getMethods• クラスのメソッド名を取得• ReflectionMethod::IS_PUBLIC etchttps://www.php.net/manual/ja/reflectionclass.getmethods.php
© - BASE, Inc.4. テストメソッドを実⾏する/src/Command.php実⾏、AssertionErrorがなければ成功(.)、あれば失敗(F)
© - BASE, Inc.hgsgtk/mpunithttps://github.com/hgsgtk/mpunit⼀旦デモ
© - BASE, Inc.xUnitを知る• Test Methodの⾒つけ⽅の⼤きく2つ• Test Discovery‧Test Enumeration• 多くのxUnitファミリーは前者、フレームワークがテストメソッドを⾒つける、ユーザーが⼿動で登録しなくていい
© - BASE, Inc.なにでアサーションするか• assert function(PHP)• (Minimumにやりたかったので)• beberlei/assert• webmozart/assert• hamcrest/hamcrest-phpetc
© - BASE, Inc.assert関連の設定をする/bin/mpunit
© - BASE, Inc.(補⾜)assert⽤の設定ディレクティブ• zend.assertions (default: )• assert()を⽣成するか‧実⾏するか• 1 の場合、コードは⽣成され実⾏される• assert.exception (default: )• アサーション失敗に指定したオブジェクトをスロー• 指定なしだとAssertionErrorhttps://www.php.net/manual/ja/function.assert.phphttps://www.php.net/manual/ja/class.assertionerror.php
© - BASE, Inc.(補⾜)assert⽤のオプション• 様々なassertフラグを設定できる• ASSERT_ACTIVE• ASSERT_WARNING• ASSERT_BAIL• ASSERT_QUIET_EVAL• ASSERT_CALLBACKhttps://www.php.net/manual/ja/function.assert-options.php
© - BASE, Inc.assertionassert関数を実⾏
© - BASE, Inc.こんな感じのテストを実⾏したいTestCaseを継承したクラスから assertSame が使えるように
© - BASE, Inc.実⾏結果
© - BASE, Inc.まとめ• ⾃作すると、xUnitがどういうことをしてくれているか分かる(ex. TestDiscovery)• テスティングフレームワークの提供者の気持ちに近づき、ユーザーの僕らのテストリテラシーも上がるのかもしれない
© - BASE, Inc.hours制作時間
© - BASE, Inc.Special Thanks: 参考資料• PHPでテスティングフレームワークを実装する前に知っておきたい勘所 / @tadsan• https://www.youtube.com/watch?v=o YY JnAOwU• sebastianbergmann/phpunit• https://github.com/sebastianbergmann/phpunit
© - BASE, Inc. Xみなさんもぜひ