PHPUnit5.0中文手册11. 代码覆盖率分析指明要覆盖的方法
上一篇:略过代码块 下一篇:边缘情况

指明要覆盖的方法

@covers 标注(参见 Table B.1, “用于指明测试覆盖哪些方法的标注”)可以用在测试代码中来指明测试方法想要对哪些方法进行测试。如果提供了这个信息,则只有指定方法的代码覆盖率信息会被统计。 Example 11.2, “在测试中指明欲覆盖哪些方法”展示了一个例子。

Example 11.2. 在测试中指明欲覆盖哪些方法


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 标注来指明一个测试不覆盖任何方法(参见the section called “@coversNothing”)。这可以在编写集成测试时用于确保代码覆盖全部来自单元测试。

Example 11.3. 指明测试不欲覆盖任何方法


addEntry("suzy", "Hello world!");
        $queryTable = $this->getConnection()->createQueryTable(
            'guestbook', 'SELECT * FROM guestbook'
        );
        $expectedTable = $this->createFlatXmlDataSet("expectedBook.xml")
                              ->getTable("guestbook");
        $this->assertTablesEqual($expectedTable, $queryTable);
    }
}
?>
上一篇:略过代码块 下一篇:边缘情况