Skip to content

Instantly share code, notes, and snippets.

@hamuhamu
Last active November 3, 2017 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamuhamu/9d17f26dbb17cd24cde3dee3948e7c99 to your computer and use it in GitHub Desktop.
Save hamuhamu/9d17f26dbb17cd24cde3dee3948e7c99 to your computer and use it in GitHub Desktop.
TDD読書会vol1 資料

TDD読書会vol1 資料

概要

「テスト駆動開発」読書会 Vol.1 の資料です。
個人の意見も反映されているので、ご注意。

資料

GitHubリポジトリ: Test_Driven_Development_By_Example PHPによるサンプルコード  

進め方

対話の時間はちゃんと設けたい。
意見や質問がいいやすい雰囲気で。  

第1部 他国通貨

  • (0. TODOリストを作る)
  • 1. まずはテストを一つ書く
  • 2. 全てのテストを走らせ、新しいテストの失敗を確認する
  • 3. 小さな変更を行う
  • 4. 全てのテストを走らせ、全て成功することを確認する
  • 5. リファクタリングを行って、重複業を除去する

2 ~ 5の繰り返し。
動作する汚いコードを動作するきれいなコードへ昇華させる。  

第1章 仮実装

まず、プルリクを作ってTODOリストを記載するだけで、要件の理解が深まる。
本書に触れられていないが、TODOリストを消化する度に自己肯定感が増しやる気が出る。

TODOリストにする粒度は、1パン(不安がなく迷いがない)で倒せるぐらいがいいと思う。  

テスト実行結果

[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 173 ms, Memory: 10.00MB

There was 1 error:

1) App\MoneyTest::testMultiplication
Error: Class 'App\Dollar' not found

/Users/hamuhamu/Test_Driven_Development_By_Example/test/MoneyTest.php:15

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
make: *** [test] Error 2

テスト実行結果

[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 166 ms, Memory: 10.00MB

There was 1 failure:

1) App\MoneyTest::testMultiplication
Failed asserting that 0 is identical to 10.

/Users/hamuhamu/Test_Driven_Development_By_Example/test/MoneyTest.php:18

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
make: *** [test] Error 1

テスト実行結果

[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 163 ms, Memory: 10.00MB

OK (1 test, 1 assertion)

短いスパンで、redとgreenを行き来するのは大事。
ちゃんとテストが動いていることを確認するために、わざとテストを壊したり騙したりすることはある。
インクリメンタルに1歩づつ着実に進めるのがTDD流。

テスト実行結果

[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 195 ms, Memory: 10.00MB

OK (1 test, 1 assertion)

ここで、初めてTODOリストの一つを消化できる。

第2章 明白な実装

  • 実装よりインターフェィスが大事
  • 最初から完璧な設計をしすぎない
    /**
     * @test
     */
    public function testMultiplication()
    {
        $five = new Dollar(5);
        $five->times(2);

        $this->assertSame(10, $five->amount);

        $five->times(3);
        $this->assertSame(15, $five->amount);
    }
[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 149 ms, Memory: 10.00MB

There was 1 failure:

1) App\MoneyTest::testMultiplication
Failed asserting that 30 is identical to 15.

/Users/hamuhamu/Test_Driven_Development_By_Example/test/MoneyTest.php:21

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
make: *** [test] Error 1

テスト実行結果

[hamuhamu@hamuhamu-no-MacBook-Air] % make test                                  
phpunit --configuration phpunit.xml test/
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 196 ms, Memory: 10.00MB

OK (1 test, 2 assertions)

第3章 三角測量

equalsメソッドのテストと実装を追加した

第4章 意図を語るテスト

テストコードtestMultiplicationのリファクタリング
amountをprivateにした

ふりかえり、今後の進め方

  • こんな感じの進め方でよかった?
  • どのぐらいの頻度で開催しようか
  • 今回は、1時間30分でしたが、時間の長さはちょうどいい?
  • 発表者は交代制、発表方式は発表者のスタイルに合わせるでOK?
  • 他にご意見あれば

次回のTDD読書会vol2 資料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment