More Related Content
More from Wataru Terada (12)
デグレを防ぐテストの書き方
- 9. ×悪い例
1 public function test_getChildren_異常系() {
2
$actual = $this->getChildren(['id' => 23]);
3
$this->assertEquals(false, $actual);
4 }
テスト通らなくなった。
23が異常系? 23のデータは正常だからfalse返さないよ。
存在しないデータのチェックかな。存在しないデータ999に変更しよう。
テスト通るようになった!!
→デグレを見逃す
(せっかくテストがあったのに
ケースの方を誤って直してしまった!!!)
- 10. ○ コメントで書く
1
2
3
4
public function test_getChildren_異常系() {
$actual = $this->getChildren(['id' => 23]); //23:parent_idがnullのデータ
$this->assertEquals(false, $actual);
}
○ メソッド名で書く
1
public function test_getChildren_parent_idがnullのデータを渡した場合() {
1
public function test_getChildren_parent_idがnullのデータを渡すとfalseが返る() {
○
1
2
3
4
データ生成モジュールを使って可読性を上げる
public function test_getChildren_異常系() {
Fabricate::create('Hoge', ['id' => 23, 'parent_id' => null]);
$actual = $this->getChildren([‘id’ => 23]);
$this->assertEquals(false, $actual);
}
- 15.
Fabricate (sizuhikoさん作/CakePHP用)
- DB上の関係ない行/列をテストから除外でき
る
- 参考URL:
http://my.opera.com/sizuhiko/blog/2013/10/12/cakephp-fabricate
public function test_getChildren_親IDがnullの場合() {
Fabricate::create('Hoge', ['id' => 23, 'parent_id' => null]);
$actual = $this->getChildren([‘id’ => 23]);
$this->assertEquals(false, $actual);
}
- 16.
@dataProvider
(PHPUnit 標準機能)
- 類似Caseを
楽に記述できる
- 参考URL:
public function provider() {
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
}
/**
* @dataProvider provider
*/
public function testAdd($a, $b, $c) {
$this->assertEquals($c, $a + $b);
}
http://phpunit.de/manual/current/ja/writing-tests-forphpunit.html#writing-tests-for-phpunit.data-providers