本文介绍了为什么PHPUnit会显示一些大括号而不被覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPUnit 3.6.7,PHP_CodeCoverage 1.1.1和Xdebug 2.1.2.当我让PHPUnit将我的代码覆盖率统计信息写入三叶草样式的XML文件时,它偶尔会显示出一个粗斜体,没有被测试覆盖.

I'm using PHPUnit 3.6.7, PHP_CodeCoverage 1.1.1, and Xdebug 2.1.2. When I have PHPUnit write my code coverage statistics to a clover-style XML file, it occasionally shows a close-curly-brace as not being covered by tests.

我在网上看到了很多关于PHPUnit何时到达"花括号的讨论,但是我不了解所发生事情的一般概念.例如,我在一行上的覆盖范围为零:

I see a lot of discussion online of when PHPUnit is "reaching" the close curly-braces, but I don't understand the general concept of what's going on. For example, I have zero coverage on one line here:

if (is_array($foo)) {
    foreach ($foo as $bar) {
        if (property_exists($bar, 'baz')) {
            return $bar;
        }
    }
}  // this line has 0 coverage
return null;

在这里:

class Foo
{
    public $myProperty;
    public function myMethod()
    {
        $this->myProperty = '1';
    }
}  // this line has 0 coverage

我项目中的其他类没有这个问题;它们的大括号根本不在XML文件中显示,因此它们没有被列为零覆盖率.

Other classes in my project don't have this problem; their close-curly-braces don't show up in the XML file at all, so they are not listed as having zero coverage.

我知道PHP_CodeCoverage 1.1.2(尚未发布)会让我在花括号后加上"//@codeCoverageIgnore"注释,但是在此之前,我想知道发生了什么,这样我就可以修正测试内容,以完全覆盖我.告诉我何时将花括号算作被遮盖"或未被遮盖"的拇指法则是什么?

I understand that PHP_CodeCoverage 1.1.2 (not released yet) will let me put a "// @codeCoverageIgnore" comment after a close curly-brace, but until that's available I want to know what's going on, so that I can fix my tests to give me complete coverage. What's the rule-of-thumb to tell me when a curly-brace should be counted as "covered" or "not covered"?

推荐答案

有一个 "Edge Cases" section in the phpunit documentation ,但这显然还不完善,因为我是在最近的查看天中了解到的:)

There is a "Edge Cases" section in the phpunit documentation but that apparently isn't complete as I've learned in the last view days :)

我个人从未见过的第二个例子失败了.我也无法复制它:我找不到无法解决问题的PHP/xDebug/PHPUnit组合. (在下面复制)

What I've personally never seen is your second example failing. I also couldn't reproduce it: I couldn't find a PHP/xDebug/PHPUnit combination where this didn't work out. (Reproduce below)

您显示的其他情况也是如此.对于所有我可以测试的两个右括号,就像人们期望的那样,它们被检测为不可执行/不可访问".

The same goes for the other case you showed. For all I could test both closing braces where detected as "not executable/reachable" just like one would expect it.

因此对于这两种情况,都不需要//@ codeCoverageIgnore或//@ codeCoverageIgnore [Start | End].

So for both of those cases no //@codeCoverageIgnore or //@codeCoverageIgnore[Start|End] should be needed.

正如@Derick在评论中建议的那样,如果要进行进一步的分析,则需要整个文件.

As @Derick suggested in the comments for any further analysis the whole file would be needed.

<?php

class Foo
{
    public $myProperty;
    public function myMethod()
    {
        $this->myProperty = '1';
    }
}

<?php

require __DIR__ . '/closingBrace.php';

class FooTest extends PHPUnit_Framework_TestCase {

    public function testMyMethod() {
        $x = new Foo();
        $x->myMethod();
    }

}

运行phpunit --coverage-text fooTest.php

Code Coverage Report 
  2012-01-12 10:17:32

 Summary: 
  Classes: 100.00% (1/1)
  Methods: 100.00% (1/1)
  Lines:   100.00% (2/2)

仅将$this->myProperty = '1';右大括号标记为可执行文件.

which only marks the $this->myProperty = '1'; it's closing brace as executable.

这篇关于为什么PHPUnit会显示一些大括号而不被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 02:06