2017-02-10 96 views
1

我可以从PhpUnit的代码覆盖率报告中隐藏私有的&受保护的方法吗?从代码覆盖率报告隐藏私有+受保护的方法?

我知道一些其他人所提出的建议,一要测试这些“间接”,但我真的不在乎,如果他们被调用与否,我认为这是时间的彻底废为我设立@covers为私人实用方法。

这里是我的phpunit.xml,如果你需要看到的是:

<phpunit 
     backupGlobals="false" 
     backupStaticAttributes="false" 
     bootstrap="vendor/autoload.php" 
     colors="true" 
     convertErrorsToExceptions="true" 
     convertNoticesToExceptions="true" 
     convertWarningsToExceptions="true" 
     processIsolation="false" 
     stopOnFailure="false" 
     syntaxCheck="false" 
     timeoutForSmallTests="1" 
     timeoutForMediumTests="10" 
     timeoutForLargeTests="60"> 

    <testsuites> 
     <testsuite name="default"> 
      <directory>./tests</directory> 
      <exclude> 
       <directory suffix=".php">./src/Internal</directory> 
      </exclude> 
     </testsuite> 
    </testsuites> 

    <filter> 
     <whitelist> 
      <directory suffix=".php">./src</directory> 
     </whitelist> 
    </filter> 

    <logging> 
     <log type="coverage-html" target="./log/codeCoverage" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/> 
     <log type="testdox-html" target="./log/testdox.html"/> 
    </logging> 
</phpunit> 

code coverage

+2

那么,如果一个公共方法依赖于私有方法,为什么要排除它们呢?这对我来说没有意义,真的:) – DonCallisto

+0

@DonCallisto只是不值得IMO的努力。收益递减。单元测试确保输出是正确的,代码覆盖率确保我测试了所有不同的场景。我真的需要确保我执行我的库中的每一行代码,即使我可能过度泛化了一些私有实用工具方法吗?我不这么认为。 – mpen

+0

那么,在这种情况下,只需关闭代码覆盖率,否则将它仅应用于公共方法imho是没有意义的。 – DonCallisto

回答

2

嘛,据我所知,这不是一个PHPUnit的功能,你必须叉php-code-coverage项目和编辑源代码。可能这不是你正在寻找的答案,但现在看来这是唯一的选择。

令人欣慰的是变化非常简单。您可以编辑CodeCoverage::getLinesToBeIgnoredmethod,添加额外的条件

if (get_class($token) == 'PHP_Token_FUNCTION') { 
    $methodVisibility = $token->getVisibility(); 

    if ($methodVisibility == 'private' || $methodVisibility == 'protected') { 
     $endLine = $token->getEndLine(); 

     for ($i = $token->getLine(); $i <= $endLine; $i++) { 
      self::$ignoredLines[$filename][$i] = TRUE; 
     } 
    } 
} 

enter image description here 方法getSomething不使用@codeCoverageIgnore或任何其他文档块被忽略。