指明要覆蓋的方法

2018-02-24 15:42 更新

指明要覆蓋的方法

@covers 標(biāo)注(參見(jiàn) Table?B.1, “用于指明測(cè)試覆蓋哪些方法的標(biāo)注”)可以用在測(cè)試代碼中來(lái)指明測(cè)試方法想要對(duì)哪些方法進(jìn)行測(cè)試。如果提供了這個(gè)信息,則只有指定方法的代碼覆蓋率信息會(huì)被統(tǒng)計(jì)。 Example?11.2, “在測(cè)試中指明欲覆蓋哪些方法”展示了一個(gè)例子。

Example?11.2.?在測(cè)試中指明欲覆蓋哪些方法

<?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());
    }
}
?>

同時(shí),可以用 @coversNothing 標(biāo)注來(lái)指明一個(gè)測(cè)試不覆蓋任何方法(參見(jiàn)the section called “@coversNothing”)。這可以在編寫(xiě)集成測(cè)試時(shí)用于確保代碼覆蓋全部來(lái)自單元測(cè)試。

Example?11.3.?指明測(cè)試不欲覆蓋任何方法

<?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);
    }
}
?>
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)