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

4

49

Requirements:

  • Netbeans with PHPUnit(6.9)
  • EDIT: Same applies, for example, to PHPStorm

How to:

  • Exclude lines from code coverage.
  • Exclude code blocks (lines) from code coverage.
Colwell answered 8/7, 2010 at 1:43 Comment(0)
A
52

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.

Assimilable answered 7/2, 2011 at 1:30 Comment(3)
I had an old version of PHPUnit so that code did not work. I figured it out but thanks. Even a better solution is to use phpunit.xml => I post answer..Colwell
Confirmed with PHPStorm: need to move the end tag after the closing bracket.Acerate
Only problem with this is that the actual classes have ugly codecoverageignore blocks in them which I dont likeMetronymic
W
89

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.

Witte answered 9/2, 2014 at 6:14 Comment(0)
A
52

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.

Assimilable answered 7/2, 2011 at 1:30 Comment(3)
I had an old version of PHPUnit so that code did not work. I figured it out but thanks. Even a better solution is to use phpunit.xml => I post answer..Colwell
Confirmed with PHPStorm: need to move the end tag after the closing bracket.Acerate
Only problem with this is that the actual classes have ugly codecoverageignore blocks in them which I dont likeMetronymic
C
3

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>
Colwell answered 7/2, 2011 at 2:18 Comment(2)
Your question asked about excluding lines and blocks which the above will not do--it ignores entire files. Also, if you use a whitelist (my company does), the blacklist is ignored.Assimilable
Oops thanks David. This question was asked so long ago and in the title it says file. But thanks for the information.Colwell
W
0

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
    }
}
Winged answered 16/1 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.