「テスト駆動開発」読書会 Vol.1 の資料です。
個人の意見も反映されているので、ご注意。
GitHubリポジトリ: Test_Driven_Development_By_Example PHPによるサンプルコード
対話の時間はちゃんと設けたい。
意見や質問がいいやすい雰囲気で。
- (0. TODOリストを作る)
- 1. まずはテストを一つ書く
- 2. 全てのテストを走らせ、新しいテストの失敗を確認する
- 3. 小さな変更を行う
- 4. 全てのテストを走らせ、全て成功することを確認する
- 5. リファクタリングを行って、重複業を除去する
2 ~ 5の繰り返し。
動作する汚いコードを動作するきれいなコードへ昇華させる。
まず、プルリクを作って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リストの一つを消化できる。
- 実装よりインターフェィスが大事
- 最初から完璧な設計をしすぎない
/**
* @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)
テストコードtestMultiplicationのリファクタリング
amountをprivateにした
- こんな感じの進め方でよかった?
- どのぐらいの頻度で開催しようか
- 今回は、1時間30分でしたが、時間の長さはちょうどいい?
- 発表者は交代制、発表方式は発表者のスタイルに合わせるでOK?
- 他にご意見あれば