简体   繁体   English

如何使用 Netbeans/PHPStorm/PHPUnit 集成从代码覆盖率中排除文件/代码块

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

Requirements:要求:

  • Netbeans with PHPUnit(6.9)带有 PHPUnit(6.9) 的 Netbeans
  • EDIT: Same applies, for example, to PHPStorm编辑:同样适用于,例如,PHPStorm

How to:如何:

  • Exclude lines from code coverage.从代码覆盖率中排除行。
  • Exclude code blocks (lines) from code coverage.从代码覆盖率中排除代码块(行)。

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:正如@david-harkness 所说,忽略个别行:

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

More information can by found in the PHPUnit Documentation under the Ignoring code blocks heading.更多信息可以在忽略代码块标题下的PHPUnit 文档中找到。

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.如果您试图实现 100% 的代码覆盖率,但有一行或多行无法测试,则可以用特殊注释将它们括起来。 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.编辑:我发现 Xdebug 通常认为右大括号是可执行的。 :( If that happens, move the end tag below it. :( 如果发生这种情况,请将结束标记移到其下方。

First make sure you have the latest and greatest phpunit or else the code ignore might be missing.首先确保您拥有最新最好的 phpunit,否则可能会丢失忽略代码。 Next create a phpunit.xml file that looks something like this:接下来创建一个phpunit.xml文件,看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM