2017-02-10 40 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或任何其他文檔塊被忽略。

相關問題