0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

テスト駆動開発 kent beck 25章 テスト駆動開発のパターン

Last updated at Posted at 2025-12-06

25章 テスト駆動開発のパターン

TDDを「運任せの書き方」ではなく、再現性のある技術として体系化すること

  1. テスト記述パターン
  2. 実装パターン
  3. リファクタリングパターン

1.テスト記述パターン

テストをどう書くかの指針
  • 失敗するテストを書く
    Red → Green → Refactor
  • 期待値を先に書く
    入力より結果を起点に設計

  • 具体例から一般化
    最初は1ケースだけ

$this->assertEquals(110, calcTax(100)); 
  • 1テスト = 1振る舞い
    責務を混ぜない

    NG
    test_tax_and_discount()
##### OK
	test_tax()
	test_discount()

2. 実装パターン

RedをGreenにするための実装戦略
  • べた書き
    酷すぎる実装でOK

function calcTax($price) {
    return 110;
}
  • 仮実装
    とりあえず定数で逃げる

    • 複雑なロジックを考えない
    • まずテストを通す
  • 三角測量
    テスト2つ以上に増やして一般化

$this->assertEquals(110, calcTax(100));
$this->assertEquals(220, calcTax(200));
定数実装が崩れる→正しい式に進化
function calcTax($price) {
    return $price * 1.1;
}
  • 偽装実装
    とりあえず嘘をつく
if ($price == 100) return 110;

 → テスト増やして破壊

3. リファクタリングパターン

 正しく動いたあと きれいにする工程

  • 重複排除
    • テスト間の重複
    • 実装ないの重複
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?