×
Copy
Open
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Slide 1
Slide 1 text
© - BASE, Inc. X ⾃作して理解するxUnit . . PHP Conference Hokkaido - @hgsgtk
Slide 2
Slide 2 text
© - BASE, Inc. : @hgsgtk Who am I? 東⼝ 和暉 ( Higashiguchi Kazuki ) Back-end Engineer (Go, PHP, Python ) BASE BANK, Inc. (BASE, Inc.) / Dev Division / Tech Lead
Slide 3
Slide 3 text
© - BASE, Inc. 毎⽇仲良しテストコード
Slide 4
Slide 4 text
© - BASE, Inc. ⾃分がPHPUnitの気持ちを知れば
Slide 5
Slide 5 text
© - BASE, Inc. もっと仲良くなれる
Slide 6
Slide 6 text
© - BASE, Inc. ⾃作して理解しよう
Slide 7
Slide 7 text
© - BASE, Inc. ところでPHPUnitとは https://phpunit.de/
Slide 8
Slide 8 text
© - BASE, Inc. xUnitとは テスティングフレームワークの総称 Kent Beck⽒が作成したSmalltalk製の SUnitが親となり、Java‧PHP .etcへ https://en.wikipedia.org/wiki/XUnit
Slide 9
Slide 9 text
© - BASE, Inc. xUnitファミリーが持っている機能 • Test Method • Assertion Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
Slide 10
Slide 10 text
© - BASE, Inc. 今⽇⾃作するもの • Test Method • Assertion Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
Slide 11
Slide 11 text
© - BASE, Inc. いざ
Slide 12
Slide 12 text
© - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit
Slide 13
Slide 13 text
© - BASE, Inc. こんな感じのテストを実⾏したい
Slide 14
Slide 14 text
© - BASE, Inc. xUnitファミリーが持っている機能 • Test Method • Assertion Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
Slide 15
Slide 15 text
© - BASE, Inc. まずは実⾏可能コマンドを作成 /bin/mpunit
Slide 16
Slide 16 text
© - BASE, Inc. Test Methodsをつくったら実⾏されるように .テストファイルを探す .テストクラスを探す .テストメソッドを探す .テストメソッドを実⾏する
Slide 17
Slide 17 text
© - BASE, Inc. 1. テストファイルを探す /src/Command.php *Test.php のファイルを集める
Slide 18
Slide 18 text
© - BASE, Inc. 2. テストクラスを探す /src/Command.php 定義済みクラスから、ユーザー定義型で MPUnit\TestCaseを 継承しているを探し出す
Slide 19
Slide 19 text
© - 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
Slide 20
Slide 20 text
© - BASE, Inc. 3. テストメソッドを探す /src/Command.php テストクラスのPUBLICメソッドから、先頭が test で始まるメ ソッドを探す
Slide 21
Slide 21 text
© - BASE, Inc. (補⾜)関数を集める • ReflectionClass::getMethods • クラスのメソッド名を取得 • ReflectionMethod::IS_PUBLIC etc https://www.php.net/manual/ja/reflectionclass.getmethods.php
Slide 22
Slide 22 text
© - BASE, Inc. 4. テストメソッドを実⾏する /src/Command.php 実⾏、AssertionErrorがなければ成功(.)、あれば失敗(F)
Slide 23
Slide 23 text
© - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit ⼀旦デモ
Slide 24
Slide 24 text
© - BASE, Inc. xUnitを知る • Test Methodの⾒つけ⽅の⼤きく2つ • Test Discovery‧Test Enumeration • 多くのxUnitファミリーは前者、フレー ムワークがテストメソッドを⾒つける、 ユーザーが⼿動で登録しなくていい
Slide 25
Slide 25 text
© - BASE, Inc. 今⽇⾃作するもの • Test Method • Assertion Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
Slide 26
Slide 26 text
© - BASE, Inc. なにでアサーションするか • assert function(PHP) • (Minimumにやりたかったので) • beberlei/assert • webmozart/assert • hamcrest/hamcrest-php etc
Slide 27
Slide 27 text
© - BASE, Inc. assert関連の設定をする /bin/mpunit
Slide 28
Slide 28 text
© - 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
Slide 29
Slide 29 text
© - 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
Slide 30
Slide 30 text
© - BASE, Inc. assertion assert関数を実⾏
Slide 31
Slide 31 text
© - BASE, Inc. こんな感じのテストを実⾏したい TestCaseを継承したクラスから assertSame が使えるように
Slide 32
Slide 32 text
© - BASE, Inc. 実⾏結果
Slide 33
Slide 33 text
© - BASE, Inc. まとめ • ⾃作すると、xUnitがどういうことをし てくれているか分かる(ex. Test Discovery) • テスティングフレームワークの提供者の 気持ちに近づき、ユーザーの僕らのテス トリテラシーも上がるのかもしれない
Slide 34
Slide 34 text
© - BASE, Inc. hours 制作時間
Slide 35
Slide 35 text
© - BASE, Inc. Special Thanks: 参考資料 • PHPでテスティングフレームワークを実装する前 に知っておきたい勘所 / @tadsan • https://www.youtube.com/ watch?v=o YY JnAOwU • sebastianbergmann/phpunit • https://github.com/sebastianbergmann/ phpunit
Slide 36
Slide 36 text
© - BASE, Inc. X みなさんもぜひ