LoginSignup
5
4

More than 5 years have passed since last update.

[ PHPUnit ] ボーリングから考えるテスト駆動開発 1テスト目

Last updated at Posted at 2016-09-24

これからはじめる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なプログラマー諸君には向いている手法なのではないか。

今後入り組んだことをやっていくにつれてどうなるのかが楽しみ。

5
4
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
5
4