2011-05-03 86 views
2

试图决定哪一个更适合我的情况。根据phpunit文档(这是非常有限的),白名单应该包括目录内的所有文件,但它似乎并没有这样工作。有没有人有任何建议,或可以指向我一个很好的参考,而不是phpunit手册。我正在使用XML配置文件。提前致谢!phpunit白名单vs黑名单

<?xml version="1.0" encoding="utf-8"?> 
<phpunit> 
    <filter> 
    <whitelist> 
     <directory suffix=".php">/home/ddohr/git/project/</directory> 
    <exclude> 
     <directory suffix=".php">/home/ddohr/git/project/vendor/</directory> 
     <directory suffix=".php">/home/ddohr/git/project/plugins/</directory> 
     <directory suffix=".php">/home/ddohr/git/project/test/</directory> 
    </exclude> 
    </whitelist> 
    </filter> 
</phpunit> 
+0

它的工作原理是这样的我:我现在使用XML文件(https://github.com/ircmaxell/PHP-CryptLib/blob/master /test/phpunit.xml.dist) – ircmaxell 2011-05-03 14:56:10

+0

排除白名单上的文件或目录意味着如果任何文件“触及”它,它将仍然被拉入,它只是不会自动将所有文件拉入?我的白名单目录中有大量文件似乎并未被拉入。 – Dave 2011-05-03 14:58:38

+0

@dave:哪里是PHPUnit根标记? testsuites标签在哪里?编辑您的问题以发布您的完整XML文件... – ircmaxell 2011-05-03 15:00:50

回答

1

更新:以下不再与上面的PHPUnit 3.6和作品。有关新解决方案,请参阅Add files to code-coverage white/blacklists in bootstrap.php for PHPUnit

原来的答案

至于“白名单黑名单VS”问题,注意它们是相互排斥的,白名单胜于黑名单。我们在项目中使用白名单,因为我们希望在没有测试的情况下报告0%的覆盖率。我们的bootstrap.php模块将白名单设置为比当时在phpunit.xml中更易于管理。

例如,库项目的bootstrap.php使用includeDirectoryForCodeCoverage()其源添加到白名单:

includeDirectoryForCodeCoverage(MY_LIBRARY_PATH); 

这是抽象的实际调用PHP_CodeCoverage一个简单的辅助:

function includeDirectoryForCodeCoverage($path) { 
    PHP_CodeCoverage_Filter::getInstance() 
      ->addDirectoryToWhitelist($path); 
} 

function includeFileForCodeCoverage($path) { 
    PHP_CodeCoverage_Filter::getInstance() 
      ->addFileToWhitelist($path); 
} 

function includeFilesForCodeCoverage(array $paths) { 
    PHP_CodeCoverage_Filter::getInstance() 
      ->addFilesToWhitelist($paths); 
} 

黑名单但是,仍然很方便。 PHPUnit将隐藏黑名单文件中任何代码的堆栈跟踪条目。对于这个原因,我加入这个小宝石:

function ignoreDirectoryInStackTraces($path) { 
    PHP_CodeCoverage_Filter::getInstance() 
      ->addDirectoryToBlacklist($path); 
} 
+0

PHPUnit 3.6中删除了“PHP_CodeCoverage_Filter :: getInstance”(并且基于非基于XML的黑名单设置成为了屁股中的一个巨大痛苦)。 – Tgr 2012-03-02 17:10:55

+0

有关在3.6.x及更高版本中运行的解决方案,请参见[将文件添加到'bootstrap.php中的代码覆盖白名单/黑名单for PHPUnit](http://stackoverflow.com/q/8085674/285873)。 – 2012-03-02 21:46:56

相关问题