【问题标题】:How to exclude files / code blocks from code coverage with Netbeans / PHPStorm / PHPUnit integration如何使用 Netbeans/PHPStorm/PHPUnit 集成从代码覆盖范围中排除文件/代码块
【发布时间】:2011-03-13 03:00:24
【问题描述】:

要求:

  • 带有 PHPUnit(6.9) 的 Netbeans
  • 编辑:同样适用于例如 PHPStorm

如何:

  • 从代码覆盖范围中排除行。
  • 从代码覆盖范围中排除代码块(行)。

【问题讨论】:

    标签: netbeans phpunit code-coverage phpstorm


    【解决方案1】:

    忽略方法代码块:

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

    忽略类代码块:

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

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

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

    更多信息可以在忽略代码块标题下的PHPUnit Documentation中找到。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 我有一个旧版本的 PHPUnit,所以代码不起作用。我想通了,但谢谢。甚至更好的解决方案是使用 phpunit.xml => 我发布答案..
      • 与 PHPStorm 确认:需要将结束标签移到右括号之后。
      • 唯一的问题是实际的类中有丑陋的 codecoverageignore 块,我不喜欢
      【解决方案3】:

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

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

      【讨论】:

      • 你的问题是关于排除上面不会做的行和块——它忽略了整个文件。此外,如果您使用白名单(我的公司使用),黑名单将被忽略。
      • 哎呀,谢谢大卫。这个问题很久以前就被问过了,在标题中它说文件。但是感谢您提供的信息。
      猜你喜欢
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 2015-07-27
      • 2013-01-01
      • 2016-03-29
      • 1970-01-01
      • 2015-06-27
      • 2018-04-05
      相关资源
      最近更新 更多