【问题标题】:Hide private + protected methods from code coverage report?从代码覆盖率报告中隐藏私有+受保护的方法?
【发布时间】:2017-06-29 06:16:15
【问题描述】:

我可以在 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>

【问题讨论】:

  • 好吧,如果一个公共方法依赖于私有方法,你为什么要排除它们呢?这对我来说毫无意义,真的:)
  • @DonCallisto 只是不值得 IMO 努力。收益递减。单元测试确保输出正确,代码覆盖率确保我测试了所有不同的场景。我是否真的需要确保执行我的库中的每一行代码,即使我可能过度概括了我的一些私有实用程序方法?我不这么认为。
  • 好吧,在这种情况下,只需关闭代码覆盖,否则仅将其应用于公共方法恕我直言是没有意义的。
  • 顺便说一句,我不是 100% 代码覆盖率的粉丝或积分主义者,但我认为如果你打开它,你应该让它评估你拥有的每一行代码
  • @DonCallisto 我想我们将不得不在那里不同意。自从我开始使用它以来,代码覆盖率帮助我发现了许多错误。这绝对有用,但我只是看不到像 __debugInfo 这样的单元测试的价值。

标签: phpunit code-coverage xdebug


【解决方案1】:

好吧,据我所知,它不是 PHPUnit 功能,您必须 fork php-code-coverage 项目并编辑源代码。可能这不是您正在寻找的答案,但似乎这是目前唯一的选择。

令人欣慰的是,这些更改非常简单。您可以编辑 CodeCoverage::getLinesToBeIgnored method 并添加额外条件

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;
       }
    }
}

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

【讨论】:

    猜你喜欢
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多