Alfred
Alfred

Reputation: 61773

How to exclude files / code blocks from code coverage with Netbeans / PHPStorm / PHPUnit integration

Requirements:

How to:

Upvotes: 50

Views: 39358

Answers (4)

Fpasquer
Fpasquer

Reputation: 413

PHP unit 9.6,

If you want to ignore only one line :

public function buildTotalLssPrice(float $discount) : float
{
    switch ($this->getProductType()) {
        case ProductLicenseOption::ORDER_TYPE_LSS_ORDER_FORM_PRODUCT_IFS:
            return $this->buildTotalLssIfsPrice($discount);
        default:
            throw new Exception(sprintf("Not defined '%s'", $this->getProductType())); // @codeCoverageIgnore
    }
}

Upvotes: 1

David Harkness
David Harkness

Reputation: 36532

If you are trying to achieve 100% code coverage but have one or more lines that you cannot test, you can surround them with special annotations. They will be ignored in the code coverage report.

if (($result = file_get_contents($url)) === false) {
    // @codeCoverageIgnoreStart
    $this->handleError($url);
    // @codeCoverageIgnoreEnd
}

Edit: I have found that Xdebug often considers the closing brace to be executable. :( If that happens, move the end tag below it.

Upvotes: 53

bnp887
bnp887

Reputation: 5746

To ignore method code blocks:

/**
 * @codeCoverageIgnore
 */
function functionToBeIgnored() {
    // function implementation
}

To ignore class code blocks:

/**
 * @codeCoverageIgnore
 */
class Foo {
    // class implementation
}

And as @david-harkness said, to ignore individual lines:

// @codeCoverageIgnoreStart
print 'this line ignored for code coverage';
// @codeCoverageIgnoreEnd

More information can by found in the PHPUnit Documentation under the Ignoring code blocks heading.

Upvotes: 91

Alfred
Alfred

Reputation: 61773

First make sure you have the latest and greatest phpunit or else the code ignore might be missing. Next create a phpunit.xml file that looks something like this:

<phpunit colors="true">
    <filter>
        <blacklist>
            <file>file1.php</file>
            <file>file2.php</file>
        </blacklist>
    </filter>
</phpunit>

Upvotes: 3

Related Questions