自作してみよう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. © - BASE, Inc. X \ ⾃作してみよう / xUnitフレームワーク .

    . PHP Conference 懇親会LT - @hgsgtk - Let’s make xUnit framework -
  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. © - BASE, Inc. Do you love PHPUnit?

  4. © - BASE, Inc. I LOVE PHPUnit

  5. © - BASE, Inc. But need more

  6. © - BASE, Inc. If you make xUnit framework

  7. © - BASE, Inc. You will love PHPUnit more!

  8. © - BASE, Inc. Let’s make it!

  9. © - BASE, Inc. By the way, PHPUnit is https://phpunit.de/

  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
  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』
  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』
  13. © - BASE, Inc. Let’s go

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

  15. © - BASE, Inc. The Goal is to execute this

    test
  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
  17. © - BASE, Inc. Make executable command /bin/mpunit

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

    .Find test files .Find test classes .Find test methods .Execute test methods
  19. © - BASE, Inc. . Find test files /src/Command.php Collect

    files “*Test.php”
  20. © - BASE, Inc. . Find test classes /src/Command.php 定義済みクラスから、ユーザー定義型で

    MPUnit\TestCaseを継承して いるを探し出す
  21. © - BASE, Inc. . Find test classes /src/Command.php Get

    declared classes
  22. © - BASE, Inc. . Find test classes /src/Command.php Is

    user defined?
  23. © - BASE, Inc. . Find test classes /src/Command.php Is

    extended MPUnit\TestCase?
  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
  25. © - BASE, Inc. . Find test methods /src/Command.php テストクラスのPUBLICメソッドから、先頭

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

    が test で始まるメソッドを探す Find public method which name starts ʻtest’
  27. © - BASE, Inc. (Tips)Gather functions • ReflectionClass::getMethods • クラスのメソッド名を取得

    • ReflectionMethod::IS_PUBLIC etc https://www.php.net/manual/ja/reflectionclass.getmethods.php
  28. © - BASE, Inc. . Execute test methods /src/Command.php If

    AssertionError => Failed (F) Else => Success (.)
  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
  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
  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
  32. © - BASE, Inc. Setting assertion options /bin/mpunit

  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
  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
  35. © - BASE, Inc. assertion Make assertion method “assertSame”

  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
  37. © - BASE, Inc. Run tests, Get report TestCaseを継承したクラスから assertSame

    が使えるように Use assertion method in test method
  38. © - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit Demonstration

  39. © - BASE, Inc. Get Report

  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.
  41. © - BASE, Inc. LOVE to PHPUnit became STRONGER!! By

    making xUnit,
  42. © - BASE, Inc. X Thanks!