自作してみようxUnitフレームワーク / try-to-self-made-xunit

自作してみようxUnitフレームワーク / try-to-self-made-xunit

PHPカンファレンス2019にて発表する懇親会LTの資料です

88964b936e864ca7d326272eaa70fa9a?s=128

Kazuki Higashiguchi

December 01, 2019
Tweet

Transcript

  1. 1.

    © - BASE, Inc. X \ ⾃作してみよう / xUnitフレームワーク .

    . PHP Conference 懇親会LT - @hgsgtk - Let’s make xUnit framework -
  2. 2.

    © - BASE, Inc. About me BASE BANK, Inc. Dev

    Division Tech Lead 『みんなのPHP 現場で役⽴つ最新 ノウハウ!』共著者 (PHPにおけるユニットテスト) https://www.amazon.co.jp/ gp/product/ @hgsgtk Kazuki Higashiguchi
  3. 10.

    © - BASE, Inc. About xUnit Generic name of testing

    framework Kent Beck: SUnit (Smalltalk) -> JUnit (Java) Sebastian Bergmann: PHPUnit (PHP) https://en.wikipedia.org/wiki/XUnit
  4. 11.

    © - BASE, Inc. Main features of xUnit family •

    Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
  5. 12.

    © - BASE, Inc. Today’s topic • Test Method •

    Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
  6. 16.

    © - BASE, Inc. • Test Method • Assertion Method

    • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic
  7. 18.

    © - BASE, Inc. Find test methods and execute them

    .Find test files .Find test classes .Find test methods .Execute test methods
  8. 24.

    © - BASE, Inc. (Tips)PHP feature to collect classes •

    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
  9. 26.

    © - BASE, Inc. . Find test methods /src/Command.php テストクラスのPUBLICメソッドから、先頭

    が test で始まるメソッドを探す Find public method which name starts ʻtest’
  10. 27.

    © - BASE, Inc. (Tips)Gather functions • ReflectionClass::getMethods • クラスのメソッド名を取得

    • ReflectionMethod::IS_PUBLIC etc https://www.php.net/manual/ja/reflectionclass.getmethods.php
  11. 28.

    © - BASE, Inc. . Execute test methods /src/Command.php If

    AssertionError => Failed (F) Else => Success (.)
  12. 29.

    © - BASE, Inc. Understand xUnit • Two strategies to

    find test functions • Test Discovery‧Test Enumeration • In Test Discovery, user does not need to register manually • Almost xUnit families’ strategy is Test Discovery
  13. 30.

    © - BASE, Inc. • Test Method • Assertion Method

    • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic
  14. 31.

    © - BASE, Inc. Ways to assert • assert function(PHP)

    • (Choice it, because I want to implement with minimum dependencies) • beberlei/assert • webmozart/assert • hamcrest/hamcrest-php etc
  15. 33.

    © - BASE, Inc. (Tips)Setting directive for assertion • 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
  16. 34.

    © - BASE, Inc. (Tips)Options for assertion • 様々なassertフラグを設定できる •

    ASSERT_ACTIVE • ASSERT_WARNING • ASSERT_BAIL • ASSERT_QUIET_EVAL • ASSERT_CALLBACK https://www.php.net/manual/ja/function.assert-options.php
  17. 36.

    © - BASE, Inc. • Test Method • Assertion Method

    • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic
  18. 37.

    © - BASE, Inc. Run tests, Get report TestCaseを継承したクラスから assertSame

    が使えるように Use assertion method in test method
  19. 40.

    © - BASE, Inc. By making xUnit, • PHPUnit has

    a lot of features, it’s amazing!! • I also red internal implementations of PHPUnit to make it. I learned a lot.