PHPUnitManual:16.2

From 흡혈양파의 번역工房
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
16.2 Test case 클래스로부터 클래스 skeleton 생성

테스트 주도 개발 ( 제12장 을 참조하세요) 에서는 테스트를 먼저 작성한 뒤에 테스트 대상 코드를 작성하게 됩니다. PHPUnit 에서는 test case 클래스를 기반으로 클래스의 skeleton 을 생성할 수 있습니다.


변환 규칙에 따라, Unit 클래스의 테스트는 UnitTest 클래스가 됩니다. 이 test case 클래스의 소스를 검색하여, Unit 클래스의 오브젝트를 참조하는 변수를 찾은 뒤, 해당 오브젝트가 어떤 메소드를 호출하는지를 조사합니다. 이 과정의 예를 예16.4 "생성된 BowlingGame 클래스의 skeleton" 을 통해 살펴 봅시다. 이 skeleton 은 예16.3 "BowlingGame 클래스" 의 분석 결과를 바탕으로 생성된 것입니다.


예16.3 BowlingGameTest 클래스

<?php
class BowlingGameTest extends PHPUnit_Framework_TestCase
{
    protected $game;
 
    protected function setUp()
    {
        $this->game = new BowlingGame;
    }
 
    protected function rollMany($n, $pins)
    {
        for ($i = 0; $i < $n; $i++) {
            $this->game->roll($pins);
        }
    }
 
    public function testScoreForGutterGameIs0()
    {
        $this->rollMany(20, 0);
        $this->assertEquals(0, $this->game->score());
    }
}
?>
phpunit-skelgen --class BowlingGameTest
PHPUnit Skeleton Generator 1.0.0 by Sebastian Bergmann.

Wrote skeleton for "BowlingGame" to "./BowlingGame.php".


예16.4 생성된 BowlingGame 클래스의 skeleton

<?php
/**
 * Generated by PHPUnit_SkeletonGenerator on 2012-01-09 at 16:55:58.
 */
class BowlingGame
{
    /**
     * @todo Implement roll().
     */
    public function roll()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }
 
    /**
     * @todo Implement score().
     */
    public function score()
    {
        // Remove the following line when you implement this method.
        throw new RuntimeException('Not yet implemented.');
    }
}
?>


작성된 클래스의 대한 테스트의 실행 결과는 다음과 같습니다.

phpunit --bootstrap BowlingGame.php BowlingGameTest
PHPUnit 3.7.0 by Sebastian Bergmann.

E

Time: 0 seconds, Memory: 3.50Mb

There was 1 error:

1) BowlingGameTest::testScoreForGutterGameIs0
RuntimeException: Not yet implemented.

/home/sb/BowlingGame.php:13
/home/sb/BowlingGameTest.php:14
/home/sb/BowlingGameTest.php:20

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.


Notes