xDebug和PHPUnit的代码覆盖率是100%,实际上不是 [英] code coverage of xDebug and PHPUnit says 100%, in fact it is not

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

问题描述

我具有以下功能:

function foo($p)
{
    if ($p == null)
        throw new Exception('bla');
    if ($p == null)
    {
        throw new Exception('bla');
    }
    [...]
}

我对此功能的测试未涵盖引发异常的行.但是PHPUnit告诉我第一个抛出"语句被覆盖,第二个没有被覆盖.也许第一个被解释了,但是没有执行.

My Test for this function is not covering the lines throwing the exception. But PHPUnit tells me the first 'throw'-statement is coverd, the second not. Maybe the first is interpreted, but it is not executed.

因此,如果我没有达到100%,我不想收到消息"100%".

So I don't want to get the message '100%' if I have not reached 100%.

这是xDebug中的bug还是我可以配置xDebug/PHPUnit?

Is this a bug in xDebug or do I have the possibility to configure xDebug/PHPUnit?

推荐答案

xDebug的代码覆盖率指标是基于语句的,而不是基于行的.这意味着将没有用大括号括起来的块的控制结构视为一条语句.要让xDebug将throw行与if()测试分开,请像在第二条语句中一样,用大括号将其包围.

xDebug's code coverage metrics are statement-based rather than line-based. What this means is that a control structure without a block enclosed in braces is treated as a single statement. To let xDebug see the throw line as separate from the if() test, surround it with braces as you did in the second statement.

if ($p == null)                    // statement 1
    throw new Exception('bla');    // rest of statement 1

vs.

if ($p == null) {                  // statement 1
    throw new Exception('bla');    // statement 2
}

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

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