Skip to content

Improvement of @covers and Introduction of @uses #140

Closed
@sebastianbergmann

Description

@sebastianbergmann

PHPUnit and PHP_CodeCoverage currently support the @covers annotation to denote which unit(s) of code a test wants to cover:

class BankAccount
{
    public function deposit(Money $money)
    {
        // ...
    }
}

class BankAccountTest extends PHPUnit_Framework_TestCase
{
    /**
     * @covers BankAccount::deposit
     */
    public function testMoneyCanBeDepositedInAccount()
    {
        // ...
    }
}

When the @covers annotation is used, any code coverage for code that is not specified using a @covers annotation but executed during the test is discarded. This code should (optionally) be marked as executed (but not as covered / tested) in the HTML code coverage report.

In Strict Mode, PHPUnit 4.0 (PHP_CodeCoverage 2.0) will fail a test that uses the @covers annotation and code is executed that is not specified using a @covers annotation. This leads to a problem when the unit of code under test uses a value object (which does not make sense to stub) as this object, too, would need to be listed using the @covers annotation in order for the test to succeed. Listing the value object using the @covers annotation, however, leads to false code coverage for the value object's code as it is not actually tested in this test.

To solve this problem, a new annotation, @uses, should be added:

class BankAccountTest extends PHPUnit_Framework_TestCase
{
    /**
     * @covers BankAccount::deposit
     * @uses   Money
     */
    public function testMoneyCanBeDepositedInAccount()
    {
        // ...
    }
}

Units of code that are listed using the @uses annotation are allowed to be executed during the test but no code coverage information will be collected for them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions