代码之家  ›  专栏  ›  技术社区  ›  Alfred

如何通过netbeans/phpsorm/phpunit集成从代码覆盖范围中排除文件/代码块

  •  17
  • Alfred  · 技术社区  · 14 年前

    要求:

    • 含phpunit的网豆(6.9)
    • 编辑: 例如,同样适用于phpsorm

    如何:

    • 从代码覆盖范围中排除行。
    • 从代码覆盖范围中排除代码块(行)。
    3 回复  |  直到 10 年前
        1
  •  24
  •   David Harkness    14 年前

    如果您试图实现100%的代码覆盖率,但有一行或多行您无法测试,您可以用特殊的注释将它们包围起来。它们将在代码覆盖率报告中被忽略。

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

    编辑: 我发现xdebug经常认为右大括号是可执行的。:(如果发生这种情况,请将结束标记移到它下面。

        2
  •  33
  •   bnp887    11 年前

    要忽略方法代码块:

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

    要忽略类代码块:

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

    正如@david harkness所说,忽略单个行:

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

    更多信息可以在 PHPUnit Documentation 忽略代码块 航向。

        3
  •  2
  •   Alfred    14 年前

    首先确保您有最新的和最好的phpunit,否则代码忽略可能会丢失。下一个创建 phpunit.xml 像这样的文件:

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