【发布时间】: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