Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
自作して理解するxUnit / self-made-xunit
Search
Kazuki Higashiguchi
September 21, 2019
Technology
2
1.8k
自作して理解するxUnit / self-made-xunit
PHPカンファレンス北海道2019で発表したxUnitを自作することで理解するというトークです
Kazuki Higashiguchi
September 21, 2019
Tweet
Share
More Decks by Kazuki Higashiguchi
See All by Kazuki Higashiguchi
インフラコストとセキュリティ課題解決のためのリアーキテクチャリング / srekaigi2025
hgsgtk
3
5k
Design of a Stateful system for Robust Deployment and Observability
hgsgtk
0
1.2k
A guide to joining operational work in your new DevOps team
hgsgtk
1
1.4k
HTTP Tunneling in Go
hgsgtk
0
1.4k
ブラウザ自動操作技術の深層へ、直接触れて学ぶ WebDriver と Chrome DevTools Protocol
hgsgtk
3
6.5k
HTTP Server on random available port in Go
hgsgtk
0
980
Agile Testingを夢見たテスト自動化 〜ATDDへの挑戦から始まる 1年間の試行錯誤〜 / dreaming agile testing at basebank
hgsgtk
14
7.8k
Create Go WebDriver client from scratch
hgsgtk
1
2.2k
PHPでWeb Driver Clientを自作する〜己の手でブラウザ操作自動化を完全理解する方法〜 / phpcon2021
hgsgtk
2
2.5k
Other Decks in Technology
See All in Technology
プロダクトエンジニア構想を立ち上げ、プロダクト志向な組織への成長を続けている話 / grow into a product-oriented organization
hiro_torii
1
340
OpenID BizDay#17 みんなの銀行による身元確認結果の活用 / 20250219-BizDay17-KYC-minna-no-ginko
oidfj
0
210
株式会社EventHub・エンジニア採用資料
eventhub
0
4.3k
コンピュータビジョンの社会実装について考えていたらゲームを作っていた話
takmin
1
570
Two Blades, One Journey: Engineering While Managing
ohbarye
3
970
設計を積み重ねてシステムを刷新する
sansantech
PRO
0
140
Classmethod AI Talks(CATs) #17 司会進行スライド(2025.02.19) / classmethod-ai-talks-aka-cats_moderator-slides_vol17_2025-02-19
shinyaa31
0
180
ソフトウェアエンジニアと仕事するときに知っておいたほうが良いこと / Key points for working with software engineers
pinkumohikan
1
140
スキルだけでは満たせない、 “組織全体に”なじむオンボーディング/Onboarding that fits “throughout the organization” and cannot be satisfied by skills alone
bitkey
0
140
AI Agent時代なのでAWSのLLMs.txtが欲しい!
watany
2
170
役員・マネージャー・著者・エンジニアそれぞれの立場から見たAWS認定資格
nrinetcom
PRO
1
3.3k
白金鉱業Meetup Vol.17_あるデータサイエンティストのデータマネジメントとの向き合い方
brainpadpr
7
990
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Visualization
eitanlees
146
15k
Why Our Code Smells
bkeepers
PRO
336
57k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
For a Future-Friendly Web
brad_frost
176
9.6k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
12
980
How GitHub (no longer) Works
holman
314
140k
Side Projects
sachag
452
42k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
193
16k
Docker and Python
trallard
44
3.3k
Transcript
© - BASE, Inc. X ⾃作して理解するxUnit . . PHP Conference
Hokkaido - @hgsgtk
© - BASE, Inc. : @hgsgtk Who 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/mpunit https://github.com/hgsgtk/mpunit
© - BASE, Inc. こんな感じのテストを実⾏したい
© - BASE, Inc. xUnitファミリーが持っている機能 • Test Method • Assertion
Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
© - 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.php https://www.php.net/manual/ja/function.get-declared-classes.php https://www.php.net/manual/ja/reflectionclass.getparentclass.php
© - BASE, Inc. 3. テストメソッドを探す /src/Command.php テストクラスのPUBLICメソッドから、先頭が test で始まるメ
ソッドを探す
© - BASE, Inc. (補⾜)関数を集める • ReflectionClass::getMethods • クラスのメソッド名を取得 •
ReflectionMethod::IS_PUBLIC etc https://www.php.net/manual/ja/reflectionclass.getmethods.php
© - BASE, Inc. 4. テストメソッドを実⾏する /src/Command.php 実⾏、AssertionErrorがなければ成功(.)、あれば失敗(F)
© - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit ⼀旦デモ
© - BASE, Inc. xUnitを知る • Test Methodの⾒つけ⽅の⼤きく2つ • Test
Discovery‧Test Enumeration • 多くのxUnitファミリーは前者、フレー ムワークがテストメソッドを⾒つける、 ユーザーが⼿動で登録しなくていい
© - BASE, Inc. 今⽇⾃作するもの • Test Method • Assertion
Method • Test Suites • Run tests, Get report 参考書籍:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』
© - BASE, Inc. なにでアサーションするか • assert function(PHP) • (Minimumにやりたかったので)
• beberlei/assert • webmozart/assert • hamcrest/hamcrest-php etc
© - BASE, Inc. assert関連の設定をする /bin/mpunit
© - 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
© - 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
© - BASE, Inc. assertion assert関数を実⾏
© - BASE, Inc. こんな感じのテストを実⾏したい TestCaseを継承したクラスから assertSame が使えるように
© - BASE, Inc. 実⾏結果
© - BASE, Inc. まとめ • ⾃作すると、xUnitがどういうことをし てくれているか分かる(ex. Test Discovery)
• テスティングフレームワークの提供者の 気持ちに近づき、ユーザーの僕らのテス トリテラシーも上がるのかもしれない
© - 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 みなさんもぜひ