これからはじめるTDD テスト駆動開発入門 ThinkIT Books より
↑はrubyでとのことだが僕はphperなのでphpでやってみた
流れとしては
①設計や行う処理を定義する
②テストを書く(失敗する)
③テストが通る処理を書く
.
.
繰り返し
ボーリングの点数計算プログラムを作る
最初の設計
- BowlingGame.phpに点数計算をさせることにする
テストを書く
<?php
use PHPUnit\Framework\TestCase;
use App\BowlingGame;
class BowlingGameTest extends TestCase
{
public $BowlingGame;
public function setUp()
{
}
public function tearDown()
{
unset($this->BowlingGame);
}
public function testCreateInstance()
{
$this->BowlingGame = new BowlingGame();
}
}
実行する
$ vendor/bin/phpunit tests/BowlingGameTest.php
怒られる
$ vendor/bin/phpunit tests/BowlingGameTest.php
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 19 ms, Memory: 4.00MB
There was 1 error:
1) BowlingGameTest::testCreateInstance
Error: Class 'App\BowlingGame' not found
/var/www/html/test/UnitTest/tests/BowlingGameTest.php:21
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
テストを通るようにする
クラスがないと言われているのでクラスファイルを作る
<?php
namespace App;
class BowlingGame
{
}
テスト実行
$ vendor/bin/phpunit tests/BowlingGameTest.php
PHPUnit 5.5.4 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 15 ms, Memory: 4.00MB
OK (1 test, 0 assertions)
クラスがあるのでもちろん通る
感想
まだ1テスト目だが、まず最初に失敗するテストを書くため
ドMなプログラマー諸君には向いている手法なのではないか。
今後入り組んだことをやっていくにつれてどうなるのかが楽しみ。