52

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.

4 Answers 4

94

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.

Sign up to request clarification or add additional context in comments.

Comments

53

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.

3 Comments

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..
Confirmed with PHPStorm: need to move the end tag after the closing bracket.
Only problem with this is that the actual classes have ugly codecoverageignore blocks in them which I dont like
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>

2 Comments

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.
Oops thanks David. This question was asked so long ago and in the title it says file. But thanks for the information.
1

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
    }
}

1 Comment

This is useful when you have previously asserted that the values entering into a switch are in the valid range, and the "correct exceptions are controlled in the assertion", nevertheless you want to leave an exception in the default: which in fact will never be reached, but you want it there. It should be a LogicException() of course as it happening is not a result of a runtime, but a direct-code-review signal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.