PHPUnitManual:14.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.
14.1 Cover 대상 메소드의 지정

@covers 선언 (annotation) ( Table B.1 "Cover 대상 메소드를 지정하기 위한 선언" 를 참조하세요) 를 사용하면, 해당 테스트 메소드가 어떤 메소드를 테스트할지를 지정할 수 있습니다. 이를 지정하면, 지정한 메소드의 Code coverage 정보만을 고려합니다.


예14.2 "대상 메소드를 지정한 테스트" 가 그 예입니다.


예14.2 대상 메소드를 지정한 테스트

<?php
require_once 'BankAccount.php';
 
class BankAccountTest extends PHPUnit_Framework_TestCase
{
    protected $ba;
 
    protected function setUp()
    {
        $this->ba = new BankAccount;
    }
 
    /**
     * @covers BankAccount::getBalance
     */
    public function testBalanceIsInitiallyZero()
    {
        $this->assertEquals(0, $this->ba->getBalance());
    }
 
    /**
     * @covers BankAccount::withdrawMoney
     */
    public function testBalanceCannotBecomeNegative()
    {
        try {
            $this->ba->withdrawMoney(1);
        }
 
        catch (BankAccountException $e) {
            $this->assertEquals(0, $this->ba->getBalance());
 
            return;
        }
 
        $this->fail();
    }
 
    /**
     * @covers BankAccount::depositMoney
     */
    public function testBalanceCannotBecomeNegative2()
    {
        try {
            $this->ba->depositMoney(-1);
        }
 
        catch (BankAccountException $e) {
            $this->assertEquals(0, $this->ba->getBalance());
 
            return;
        }
 
        $this->fail();
    }
 
    /**
     * @covers BankAccount::getBalance
     * @covers BankAccount::depositMoney
     * @covers BankAccount::withdrawMoney
     */
 
    public function testDepositWithdrawMoney()
    {
        $this->assertEquals(0, $this->ba->getBalance());
        $this->ba->depositMoney(1);
        $this->assertEquals(1, $this->ba->getBalance());
        $this->ba->withdrawMoney(1);
        $this->assertEquals(0, $this->ba->getBalance());
    }
}
?>


특정 테스트에 대해, 어떤 메소드도 테스트하지 않도록 지정할 수도 있습니다. 이 경우에는 @coversNothing 선언을 사용할 수 있습니다 ( "@coversNothing" 을 참조하세요). 결합 테스트를 작성하는 도중에, 하나의 단위 테스트에 대한 Code coverage 를 생성할 때 유용합니다.


예14.3 어떤 메소드도 테스트하지 않도록 지정된 테스트

<?php
class GuestbookIntegrationTest extends PHPUnit_Extensions_Database_TestCase
{
    /**
     * @coversNothing
     */
    public function testAddEntry()
    {
        $guestbook = new Guestbook();
        $guestbook->addEntry("suzy", "Hello world!");
 
        $queryTable = $this->getConnection()->createQueryTable(
            'guestbook', 'SELECT * FROM guestbook'
        );
        $expectedTable = $this->createFlatXmlDataSet("expectedBook.xml")
                              ->getTable("guestbook");
        $this->assertTablesEqual($expectedTable, $queryTable);
    }
}
?>


Notes