【问题标题】:Interfaces and empty classes in code coverage代码覆盖中的接口和空类
【发布时间】:2016-03-10 15:16:14
【问题描述】:

我正在使用 PHPUnit 5.2.9、PHP 5.6.19 和 Xdebug 2.4.0(非 RC)以及 netbeans IDE。

像任何其他项目一样,我使用接口和奇怪的空扩展类。由于这些文件不包含可执行代码,为什么它们会列在我的代码覆盖率报告中?不仅如此,它们被列为 0% 覆盖的 0/0 方法。 (如果那是 100% 只是为了减少红色,我会很高兴)

我已尝试在 phpunit.xml 文件中排除它们:

<whitelist processUncoveredFilesFromWhitelist="false"> // true make no difference
    <directory suffix=".php">./Annotation</directory>
    <directory suffix=".php">./Cache</directory>
    <exclude>
        <directory suffix=".php">./*Interface</directory>
        <directory suffix=".php">./*/*Interface</directory>
    </exclude>
</whitelist>

但似乎 glob (*) 仅对目录有效。但是,我可以使用 &lt;file&gt; 标签并单独排除它们。但是,有很多文件一开始就不应该被包括在内时要排除。

我做错了什么还是这是标准行为?

【问题讨论】:

  • 你实现了那些接口吗?在我的覆盖界面上显示为绿色...
  • @JakubZalas 是的,它们都是我的。在我看来,它们应该是绿色的,但它们被 0% 覆盖并且是红色的。您使用的是 netbeans 以及 PHP Xdebug 的哪些版本?

标签: php phpunit code-coverage xdebug


【解决方案1】:

您可以尝试如下改进您的排除目录:

<exclude>
    <directory suffix="Interface.php">.</directory>
</exclude>

否则,您可以直接在忽略代码Blocks of the doc中描述的代码中正确标注,如下:

<?php
/**
 * @codeCoverageIgnore
 */
interface Foo
{
    public function bar();
}

或者您可以在测试用例中将接口类的方法标记为已覆盖,请参阅更多信息in the doc here。例如:

 /**
 * @test
 * @covers Bar::foo
 * @covers BarInterface::foo
 */
  public function foo()
  {
    ....
  }

希望有帮助

【讨论】:

  • 您的排除方法完美无缺。但是,@codeCoverageIgnore@covers 都没有做任何事情。这些接口仍然包含在 xml 输出中,netbeans 将它们显示为 0% 的覆盖率。根据上面@JakubZalas 的评论,我认为这是netbeans 本身的一个错误。
  • 嗨@twifty 你用的是什么版本的phpinit?
  • 它在 OP 中,5.2.9
  • 嗨@Twifty 对不起,我现在看到了。关于注释,我看到关于它的问题是指 PHP_CodeCoverage 库,所以我不知道是由于 phpunit 还是 xdebug。
猜你喜欢
  • 2012-11-21
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2013-11-14
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多