首页 文章

PHPUnit HTML和Clover覆盖率报告因codeCoverageIgnore而异

提问于
浏览
3

我正在使用PHPUnit 3.5.14并且有一套测试,它覆盖了我的PHP应用程序的100%,不包括// @codeCoverageIgnore [Start | End]的某些部分 . HTML覆盖率报告显示100%的覆盖率 . 但是,当我生成一个Clover XML覆盖率报告时,我希望Jenkins读取该报告以强制执行100%覆盖率要求,它会显示我所忽略的所有代码 .

例如,我有一个包含20个方法的控制器类,其中一个看起来像这样:

// @codeCoverageIgnoreStart
/**
 * Gets an instance of Foo.  Abstracted for testing.
 *
 * @param array $options The constructor argument
 *
 * @return Foo
 */
protected function _getFoo(array $options)
{
    return new Foo($options);
}
// @codeCoverageIgnoreEnd

HTML覆盖率报告显示了涵盖的20个方法,包括完全忽略的方法:

pic:报道摘录

enter image description here
http://i.imgur.com/VRtKR.png

但是Clover XML报告显示了19/20方法,并没有提到_getFoo:

<class name="CampaignController" namespace="global" (...)>
  <metrics methods="20" coveredmethods="19" conditionals="0" coveredconditionals="0" statements="532" coveredstatements="532" elements="552" coveredelements="551"/>

...

<line num="592" type="stmt" count="1"/>
  <line num="593" type="stmt" count="1"/>
  <line num="615" type="method" name="createAction" crap="2" count="2"/>
  <line num="617" type="stmt" count="2"/>

(顶部的_getFoo行是第596-608行 . )

我的PHPUnit配置的日志记录部分如下所示:

<logging>
    <log type="coverage-html" target="../public/build/coverage" charset="UTF-8"
        yui="true" highlight="true" lowUpperBound="90" highLowerBound="100"/>
    <log type="coverage-clover" target="../public/build/test-coverage.xml"/>
</logging>

Is there a way to configure the Clover coverage log entry, or change my coverage ignore comments, so that the Clover report indicates 100% coverage to match the HTML report?

1 回答

  • 2

    问题出在 PHP_CodeCoverage_Report_Clover::process() . 虽然在添加方法中的行数时它正确地忽略了标记的行,但它从 PHP_Token_Stream 获取了不知道那些代码覆盖率注释的方法列表 . 我在github上创建了issue #54,它应该相对容易修复 .

    阅读 PHP_CodeCoverage_Report_Clover 使用的 PHP_CodeCoverage::getLinesToBeIgnored() ,看来您可以通过向其docblock添加 @codeCoverageIgnore 来忽略整个类或方法 .

    /**
     * Gets an instance of Foo.  Abstracted for testing.
     *
     * @param array $options The constructor argument
     * @return Foo
     *
     * @codeCoverageIgnore
     */
    

    虽然这不能解决问题,但比使用匹配的 // 注释更容易 .

    Update: 如果您想尝试修复,请使用此修改版本替换 PHP_CodeCoverage_Report_Clover::process() 内的方法的 foreach 循环 .

    foreach ($_class['methods'] as $methodName => $method) {
        $methodCount        = 0;
        $methodLines        = 0;
        $methodLinesCovered = 0;
    
        for ($i  = $method['startLine'];
             $i <= $method['endLine'];
             $i++) {
            if (isset($ignoredLines[$i])) {
                continue;
            }
    
            $add   = TRUE;
            $count = 0;
    
            if (isset($files[$filename][$i])) {
                if ($files[$filename][$i] != -2) {
                    $classStatistics['statements']++;
                    $methodLines++;
                }
    
                if (is_array($files[$filename][$i])) {
                    $classStatistics['coveredStatements']++;
                    $methodLinesCovered++;
                    $count = count($files[$filename][$i]);
                }
    
                else if ($files[$filename][$i] == -2) {
                    $add = FALSE;
                }
            } else {
                $add = FALSE;
            }
    
            $methodCount = max($methodCount, $count);
    
            if ($add) {
                $lines[$i] = array(
                  'count' => $count,
                  'type'  => 'stmt'
                );
            }
        }
    
        if ($methodLines > 0) {
            $classStatistics['methods']++;
    
            if ($methodCount > 0) {
                $classStatistics['coveredMethods']++;
            }
    
            $lines[$method['startLine']] = array(
              'count' => $methodCount,
              'crap'  => PHP_CodeCoverage_Util::crap(
                           $method['ccn'],
                           PHP_CodeCoverage_Util::percent(
                             $methodLinesCovered,
                             $methodLines
                           )
                         ),
              'type'  => 'method',
              'name'  => $methodName
            );
        }
    }
    

相关问题