English 中文(简体)
How to exclude files / code blocks from code coverage with Netbeans / PHPStorm / PHPUnit integration
原标题:

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.
最佳回答

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.

问题回答

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.

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>




相关问题
Netbeans CVS - existing repo - existing working copy

I m using Netbeans to develop with Drupal. I m trying to let Netbeans get drupal core and modules from the repository on drupal.org to my local working copy. Problem is: I already have a working ...

Java Library and Class Path Problems

Quick personal background: I was hired a few months ago as the sole .NET developer (C#) by a company whose other devs are all php devs. A week into the job I was told they wanted to switch to Java ...

Privileged operations in netbeans mobility

I m writing a Java ME app that will use privileged operations such as messaging. By default the user is prompted to confirm each of these operations, but I would like to run it as a background ...

Running unit tests on both windows and linux

Is there a way, a method, to be able to effectively run unit tests (phpunit) on both linux and windows? I need to do this because some parts of the system is only available under linux, but i do want ...

Dependency bundle (jar-files/sources/API docs) in Eclipse

I m developing various in-house extensions for JIRA, the issue tracker we use. So far I worked with Netbeans and everything worked like a charm. However, now I need to switch to Eclipse and I m ...

热门标签