PHPUnitManual:16.1

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.1 Test Case 클래스의 Skeleton 을 생성

기존 코드의 테스트를 만들 때는, 다음과 같은 코드를 여러번 반복하여 쓰게 될 것입니다.

public function testMethod()
{
}


PHPUnit Skeleton Generator 는 기존 클래스의 코드를 분석하여, 테스트 클래스의 skeleton 을 만들 수 있습니다.


예16.1 Calculator 클래스

<?php
class Calculator
{
    public function add($a, $b)
    {
        return $a + $b;
    }
}
?>


다음 예는, Calculator ( 예16.1 "Calculator 클래스" 를 참조하세요) 클래스를 위한 테스트 클래스의 skeleton 을 만드는 과정입니다.

phpunit-skelgen --test Calculator
PHPUnit Skeleton Generator 1.0.0 by Sebastian Bergmann.

Wrote skeleton for "CalculatorTest" to "/home/sb/CalculatorTest.php".


클래스의 각 메소드에 대해, 작성된 test case 클래스의 test case 는 불완전한 상태 (제9장 을 참조하세요) 입니다.


Namespace 를 가지는 클래스와 Skeleton Generator
Namespace 안에서 선언된 클래스를 테스트하기 위한 코드를 Skeleton Generator 로 생성할 때는, 해당 클래스의 qualified name 과, 클래스가 선언되어 있는 소스 파일의 경로를 넘겨야 합니다.
예를 들어, namespace Project 에 Calculator 클래스가 선언되어 있는 경우, 다음과 같이 Skeleton Generator 를 실행합니다.
phpunit-skelgen --test -- "project\Calculator" Calculator.php
PHPUnit Skeleton Generator 1.0.0 by Sebastian Bergmann.

Wrote skeleton for "project\CalculatorTest" to "/home/sb/CalculatorTest.php".


생성된 테스트 클래스를 실행한 결과는 다음과 같습니다.

phpunit --bootstrap Calculator.php --verbose CalculatorTest
PHPUnit 3.7.0 by Sebastian Bergmann.

I

Time: 0 seconds, Memory: 3.50Mb

There was 1 incomplete test:

1) CalculatorTest::testAdd
This test has not been implemented yet.

/home/sb/CalculatorTest.php:38
OK, but incomplete or skipped tests!
Tests: 1, Assertions: 0, Incomplete: 1.


@assert 선언을 메소드의 주석에 사용하여, 단순하지만 의미를 가지는 테스트를 자동 생성할 수 있습니다. 이는 불완전한 test case 가 아닙니다. 예16.2 "@assert 선언을 사용한 Calculator 클래스" 가 그 예입니다.


예16.2 @assert 선언을 사용한 Calculator 클래스

<?php
class Calculator
{
    /**
     * @assert (0, 0) == 0
     * @assert (0, 1) == 1
     * @assert (1, 0) == 1
     * @assert (1, 1) == 2
     */
    public function add($a, $b)
    {
        return $a + $b;
    }
}
?>


기존 클래스의 각 메소드에 대해, @assert 선언의 내용을 체크합니다. 그 결과, 다음과 같은 테스트 코드가 생성됩니다.

    /**
     * Generated from @assert (0, 0) == 0.
     */
    public function testAdd() {
        $o = new Calculator;
        $this->assertEquals(0, $o->add(0, 0));
    }


생성된 test case 클래스의 실행 결과는 다음과 같습니다.


phpunit --bootstrap Calculator.php --verbose CalculatorTest
PHPUnit 3.7.0 by Sebastian Bergmann.

....

Time: 0 seconds, Memory: 3.50Mb

OK (4 tests, 4 assertions)



Notes