使用 PHPUnit 达到 100% 的代码覆盖率 [英] Reaching 100% Code Coverage with PHPUnit

查看:39
本文介绍了使用 PHPUnit 达到 100% 的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为一个项目创建一个测试套件,虽然我意识到获得 100% 的覆盖率不是应该努力的指标,但在我想要澄清的代码覆盖率报告.

I've been in the process of creating a test suite for a project, and while I realize getting 100% coverage isn't the metric one should strive to, there is a strange bit in the code coverage report to which I would like some clarification.

看截图:

因为被测试方法的最后一行是 return,最后一行(只是一个右括号)显示为从未被执行,因此整个方法被标记概述中未执行.(要么那样,要么我没有正确阅读报告.)

Because the last line of the method being tested is a return, the final line (which is just a closing bracket) shows up as never being executed, and as a consequence the whole method is flagged as not executed in the overview. (Either that, or I'm not reading the report correctly.)

完整方法:

static public function &getDomain($domain = null) {
    $domain = $domain ?: self::domain();

    if (! array_key_exists($domain, self::$domains)) {
        self::$domains[$domain] = new Config();
    }

    return self::$domains[$domain];
}

这是有原因的,还是故障?

Is there a reason for this, or is it a glitch?

(是的,我通读了如何获得 100% 的代码覆盖率使用 PHPUnit,不同的情况虽然相似.)

(Yes, I read through How to get 100% Code Coverage with PHPUnit, different case although similar.)

费力地阅读报告,我注意到代码中其他位置的 switch 语句也是如此.所以这种行为至少在某种程度上是一致的,但对我来说仍然令人困惑.

Trudging on through the report, I noticed the same is true for a switch statement elsewhere in the code. So this behaviour is at least to some extent consistent, but baffling to me none the less.

编辑 2:

我在 OS X 上运行:PHPUnit 3.6.7、PHP 5.4.0RC5、XDebug 2.2.0-dev

I'm running on: PHPUnit 3.6.7, PHP 5.4.0RC5, XDebug 2.2.0-dev on a OS X

推荐答案

首先:100% 的代码覆盖率是努力的重要指标.只是通过合理的努力并不总是可以实现的,而且这样做并不总是很重要:)

First off: 100% code coverage is a great metric to strive for. It's just not always achievable with a sane amount of effort and it's not always important to do so :)

对于简单的情况,xDebug 可以判断该行无法访问,因此您可以在那里获得 100% 的代码覆盖率.

For simple cases xDebug can tell that the line is NOT reachable so you get 100% code coverage there.

请参阅下面的简单示例.

问题现已解决xDebug bugtracker 因此构建新版本的 xDebug 将解决这些问题:)

The issue is now fixed xDebug bugtracker so building a new version of xDebug will solve those issues :)

由于您运行的是 PHP 5.4 和 xDebug 的 DEV 版本,因此我已经安装并测试了它们.我遇到了和你一样的问题,你评论了同样的输出.

Since you are running PHP 5.4 and the DEV version of xDebug I've installed those and tested it. I run into the same issues as you with the same output you've commented on.

我不能 100% 确定问题是否来自 xDebug 的 php-code-coverage(phpunit 模块).这也可能是 xDebug dev 的问题.

I'm not a 100% sure if the issue comes from php-code-coverage (the phpunit module) for xDebug. It might also be an issue with xDebug dev.

我向 php-code-coverage 提交了一个错误 我们会找出问题的根源.

I've filed a bug with php-code-coverage and we'll figure out where the issue comes from.

对于更复杂的情况,此可以失败.

For more complex cases this CAN fail.

对于你展示的代码,我只能说它对我有用"(下面的复杂示例).

For the code you showed all I can say is that "It works for me" (complex sample below).

我已经看到它在当前版本中失败,但这取决于有时整个类的外观.

I've seen it fail with current versions but it depends on how the whole class looks sometimes.

删除 ?: 运算符和其他单行多语句内容也可能有所帮助.

Removing ?: operators and other single-line multi-statement things might also help out.

据我所知,xDebug 正在进行重构以避免更多这些情况.xDebug 曾经希望能够提供语句覆盖",这应该可以解决很多这样的情况.目前在这里可以做的不多

There is ongoing refactoring in xDebug to avoid more of those cases as far as I'm aware. xDebug once wants to be able to provide "statement coverage" and that should fix a lot of those cases. For now there is not much one can do here

虽然 //@codeCoverageIgnoreStart//@codeCoverageIgnoreEnd 会覆盖"这一行,但它看起来真的很丑,而且通常做得比好处多.

While //@codeCoverageIgnoreStart and //@codeCoverageIgnoreEnd will get this line "covered" it looks really ugly and is usually doing more bad than good.

对于发生这种情况的另一种情况,请参阅以下问题和答案:

For another case where this happens see the question and answers from:

what-to-do-when-project-coding-standards-conflicts-with-unit-test-code-coverage

<?php
class FooTest extends PHPUnit_Framework_TestCase {
    public function testBar() {
        $x = new Foo();
        $this->assertSame(1, $x->bar());
    }
}

<?php
class Foo {
    public function bar() {
        return 1;
    }
}

产生:

phpunit --coverage-text mep.php 
PHPUnit 3.6.7 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.50Mb

OK (1 test, 1 assertion)

Generating textual code coverage report, this may take a moment.

Code Coverage Report 
  2012-01-10 15:54:56

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

Foo
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  1/  1)

复杂的例子:

<?php

require __DIR__ . '/foo.php';

class FooTest extends PHPUnit_Framework_TestCase {

    public function testBar() {
        $this->assertSame('b', Foo::getDomain('a'));
        $this->assertInstanceOf('Config', Foo::getDomain('foo'));
    }
}

<?php

class Foo {
    static $domains = array('a' => 'b');

    static public function &getDomain($domain = null) {
        $domain = $domain ?: self::domain();
        if (! array_key_exists($domain, self::$domains)) {
            self::$domains[$domain] = new Config();
        }
        return self::$domains[$domain];
    }
}

class Config {}

产生:

PHPUnit 3.6.7 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.50Mb

OK (1 test, 2 assertions)

Generating textual code coverage report, this may take a moment.

Code Coverage Report 
  2012-01-10 15:55:55

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

Foo
  Methods: 100.00% ( 1/ 1)   Lines: 100.00% (  5/  5)

这篇关于使用 PHPUnit 达到 100% 的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆