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

查看:128
本文介绍了使用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.

Edit2:

我运行的是: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.

我不确定该问题是否来自xDebug的php-code-coverage(phpunit模块). xDebug开发人员也可能有问题.

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.

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

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天全站免登陆