Slide 1

Slide 1 text

ユニットテスト初心者を 脱するために身に着けたい N 個のこと #phpstudy 第135回PHP勉強会@東京 Kazuki Higashiguchi (@hgsgtk) 1

Slide 2

Slide 2 text

2 こんな経験ありませんか?

Slide 3

Slide 3 text

3 このテストは微妙ですね。 この書き方ではなくてこういう書き方にしましょ う。

Slide 4

Slide 4 text

4 結局、これは何をテストしてるの・・・?

Slide 5

Slide 5 text

5 何も触ってないのに、あなたの書いたテスト コードが落ちました。

Slide 6

Slide 6 text

Talk Theme このトークで解決したいこと 6 ● 身近な誰かに諭されない、「使いこなし た」テストを書けること ● 未来の誰かが見て、「読める」テストを書 けること ● 未来の誰かに怒られない、「壊れにくい」 テストを書けること

Slide 7

Slide 7 text

@hgsgtk Kazuki Higashiguchi job is … Software Engineer lang is ... PHP, Go ...etc belongs to ... BASE BANK株式会 社 (BASE株式会社の100%子会社) at #phpstudy talked about … CakePHP4.x / terraform / ECS / Unit Testing 7

Slide 8

Slide 8 text

= 8 BASE BANK Mission 「銀行をかんたんにし、全ての人が挑戦できる世の中に」 即座に資金調達ができる金融サービス「YELL BANK(エールバンク)」 https://thebase.in/yellbank

Slide 9

Slide 9 text

= 1. What is Unit testing? 2. Learn useful functions of PHPUnit 3. Clarify the intent of the test 4. Tear down completely 9 N=0; N++;

Slide 10

Slide 10 text

1. What is Unit Testing? 10

Slide 11

Slide 11 text

= ソフトウェアにまつわる様々なテスト ● 単体テスト(Unit Testing) ● 結合テスト(Integration Testing) ● システムテスト(System testing) ● 運用テスト(Implementation Testing) ● 障害テスト(Recovery Testing) ● 耐久テスト(Duration Testing) ● パフォーマンステスト(Performance Testing) 11 Software Testing ways

Slide 12

Slide 12 text

Unit Testing ~ One of Testing ways ~ 12 WHAT ● Unit = アプリケーションのテスト可能な 最小の部品単位 ● クラス・メソッドに対するテスト ● 主にプログラマによって作成される WHY ● バグを見つける ● テスト対象のドキュメンテーション ● 設計の欠陥を明らかにする ...etc

Slide 13

Slide 13 text

Testing Framework ユニットテストの方法 13 ● PHPUnit ○ https://phpunit.de/ ○ PHPUnit is a programmer-oriented testing framework for PHP. ○ xUnit architecture ● composer require --dev phpunit/phpunit ^7

Slide 14

Slide 14 text

= ● xUnit Architecture ○ https://en.wikipedia.org/wiki/XUnit ○ https://www.martinfowler.com/bliki/Xunit.html ○ All xUnit frameworks share the following basic component architecture ● basic components ○ Test Runner ○ Test Case ○ Test Fixtures ○ Test Suites ○ Test Execution (setup / teardown) ○ Test Result Formatter ○ Assertions 14 xUnit architecture

Slide 15

Slide 15 text

2. Learn useful functions of PHPUnit 15

Slide 16

Slide 16 text

16 このテストは微妙ですね。 この書き方ではなくてこういう書き方にしましょ う。

Slide 17

Slide 17 text

= ● setUp / tearDown ○ テストクラス共通の前準備・後片付けを定義する ● assertEquals / assertSame ○ 値比較 ● @expectedException and @expectedExceptionMessage ○ Exceptionのアサーション ● @dataProvider ○ テストケースをテストメソッドから分離 17 basic functions of PHPUnit

Slide 18

Slide 18 text

setup / teardown 18

Slide 19

Slide 19 text

assertSame / assertEquals 19 ● differences ○ assertEquals => ‘==’ ○ assertSame => ‘===’ ● assertSame is stricter.

Slide 20

Slide 20 text

assert exception @expectedException @expectedExceptionMessage 20

Slide 21

Slide 21 text

dataProvider @dataProvider 21

Slide 22

Slide 22 text

3. Clarify the intent of the test 22

Slide 23

Slide 23 text

23 結局、これは何をテストしてるの・・・?

Slide 24

Slide 24 text

= ● テストの意図を明確にする HOW ● テストメソッド名を明確にする ● テストケース名を明確にする ● テスト対象をannotationで明示する ● テスト対象を絞る (Optional) 24 Clarify the intent of the test

Slide 25

Slide 25 text

テストメソッド名 を明確にする ● 「何を確認するのか」を明 確に ● 場合によっては、日本語の テストメソッドもOK 25

Slide 26

Slide 26 text

テストケース名を 明確に ● 「どういうケースなのか」 を明確に ● 場合によっては、日本語の テストケース名もOK 26 ● dataProviderのキー名にケース名を指定 ● テスト実行時に指定したキー名をPHPUnitが認識する

Slide 27

Slide 27 text

テスト対象を annotationで明示 する 27 アノテーションでテスト対象を明示する ● @see ● @covers ex. `@see ArticlesController::view()`

Slide 28

Slide 28 text

テスト対象を絞る ● 一つのテストケース内の Assertionの数を絞る ● (ただし、これは流派によ る) 28

Slide 29

Slide 29 text

4. Tear down completely 29

Slide 30

Slide 30 text

30 何も触ってないのに、あなたの書いたテスト コードが落ちました。

Slide 31

Slide 31 text

= ● 自分のテストが他のテストに影響を与えうることを認識する ○ PHPUnitは一回の実行プロセス(parallelだと話は別) ○ グローバルな状態変化の影響を受ける ● Example ○ テスト時間(timecop) ○ トランザクション ● テストケース内・tearDownで確実にお片付け 31 Tear down completely

Slide 32

Slide 32 text

= ● Testing the function that contains external service ● Test Driven Development ● Test Coverage ● Sprout class/method ...etc 32 N++;

Slide 33

Slide 33 text

N++ @PHPerKaigi 2019 33

Slide 34

Slide 34 text

Fin. 34